Below I have table – Company
id name value year
1 IBM 10 2011
2 IBM 30 2012
3 IBM 10 2012
4 C 10 2010
I want to group records by name and from each group return only one record with maximum id. All results combine into the list of companies using linq where year is greater 2011. For my example output should be – “3 IBM 10 2012”
I did write something but does not working.
var a = from x in companies where x.year > 2011
group x by new {x.name, x.value, x.ID, x.year } into g
select new {
g.Key.name,
g.Key.value,
g.Max(a=>a.ID),
g.Key.value
};
return a.ToList();
Try this:
Or something more efficient: