I have a list of Umbraco nodes with properties 'level', 'section', 'theme' and 'date', with the first three being strings, the last being a DateTime.
I need my list filtered so that each entry is unique based on a composite of 'level', 'section' and 'them'. however were duplicates are found, the node selected to remain in the list should be the node with the earliest date.
I've being trying to figure this out using LINQ and Lambdas but have finally hit a wall after a few hours.
Example
pre-filtering
level section theme date
a b c 01/02/2003
d b c 01/04/2003
a b c 23/02/2015
d b c 12/12/2003
d b c 28/10/2003
post-filtering
level section theme date
a b c 01/02/2003
d b c 01/04/2003
Group by level, section and theme. Where there is more than one entry, select the entry with the earliest date.
Probably pretty simple but I'm new to C#/LINQ. Any help appreciated.
This should be what you are looking for:
var ans1 = from s in src
group s by new { s.level, s.section, s.theme } into sg
select new { sg.Key.level, sg.Key.section, sg.Key.theme, date = sg.Min(s => s.date) };
GroupBy
is often easier to understand in query comprehension, but if you prefer lambda syntax
var ans2 = src.GroupBy(s => new { s.level, s.section, s.theme }, (skey, sg) => new { skey.level, skey.section, skey.theme, date = sg.Min(s => s.date) });