How to find duplicate property values in an IList of objects and merge them?
The situation is as follows:
In a school where there is a certain class that is divided into 2 groups:
The First is studying English and the other one German, however the time allocated for the classes is the same.
I have the virtual situation where in the IList
(holding a record from the DB) I have the same Time
class for the same class of Students and I need to join these objects using C# and preferably LINQ.
In other words, I need to merge one object into another when these objects have the same Time property. The final result will be displayed in the school schedule (timetable) and the problem is to avoid having duplicated records.
I currently have something like:
var lessons = KlassLesson.GetKlassSchedule(userProfile.Preferences.MyClassID, dayOfWeek);
lessons = JoinDoubleLessons(lessons);
private List<KlassLesson> JoinDoubleLessons(List<K开发者_如何转开发lassLesson> lessons)
{
// ?????
}
I should say that I'm not very clear about your question and since you've not answered my questions I've assumed that there is only one list and that this list contains some duplicate "AddedDate" values
The following is completely functional code. I set up a few lessons. Note that I've assigned two of these lessons the AddedDate value from the variable "testDateAndTime". The Lessons with Titles AAAAA and BBBBB
class Program
{
static void Main(string[] args)
{
var testDateAndTime = DateTime.Now;
var lessons = new Lesson[] {
new Lesson { Title="A", Subject="English", AddedDate = DateTime.Now.AddMinutes(1)},
new Lesson { Title="AA", Subject="English", AddedDate = DateTime.Now.AddDays(2)},
new Lesson { Title="AAA", Subject="English", AddedDate = DateTime.Now.AddDays(3)},
new Lesson { Title="AAAA", Subject="English", AddedDate = DateTime.Now.AddDays(4)},
new Lesson { Title="AAAAA", Subject="English", AddedDate = testDateAndTime},
new Lesson { Title="B", Subject="German", AddedDate = DateTime.Now.AddMinutes(5)},
new Lesson { Title="BB", Subject="German", AddedDate = DateTime.Now.AddDays(6)},
new Lesson { Title="BBB", Subject="German", AddedDate = DateTime.Now.AddDays(7)},
new Lesson { Title="BBBB", Subject="German", AddedDate = DateTime.Now.AddDays(8)},
new Lesson { Title="BBBBB", Subject="German", AddedDate = testDateAndTime}
};
var groupedLessons = from l in lessons
group l by l.AddedDate into g
select new { AddedDate = g.Key, Lessons = g };
foreach (var k in groupedLessons)
{
Console.WriteLine("Added Date: " + k.AddedDate);
foreach (var l in k.Lessons)
{
Console.WriteLine("\t Lesson Title: " + l.Title);
}
}
}
}
public class Lesson
{
public string Title { get; set; }
public string Subject { get; set; }
public DateTime AddedDate { get; set; }
}
The output I get is:
Added Date: 2/2/2011 3:25:36 AM
Lesson Title: A
Added Date: 2/4/2011 3:24:36 AM
Lesson Title: AA
Added Date: 2/5/2011 3:24:36 AM
Lesson Title: AAA
Added Date: 2/6/2011 3:24:36 AM
Lesson Title: AAAA
Added Date: 2/2/2011 3:24:36 AM
Lesson Title: AAAAA
Lesson Title: BBBBB
Added Date: 2/2/2011 3:29:36 AM
Lesson Title: B
Added Date: 2/8/2011 3:24:36 AM
Lesson Title: BB
Added Date: 2/9/2011 3:24:36 AM
Lesson Title: BBB
Added Date: 2/10/2011 3:24:36 AM
Lesson Title: BBBB
Which shows two lessons (AAAAA and BBBBB) for the added date of 2/2/2011 3:24:36 AM which is what I expect. But then again, I'm not sure what you're expecting. I'm not sure about what you mean by "merge". What you have with this solution is that duplicates have been identified (and grouped) so you can now take the duplicates and "merge" them any way you see fit.
You could modify the sql expression such that you get only the duplicates like so:
var groupedLessons = from l in lessons
group l by l.AddedDate into g
where g.Count() > 1
select new { AddedDate = g.Key, Lessons = g };
精彩评论