同じ id (GroupBy) のグループを抽出し、次に SelectMany を使用して各グループの最大 No をエクスポートする必要があります。
public void Exec()
{
var items = new List<Item>{
new Item{ No = 1, Id = 1, Name = "A", Status = 1} ,
new Item{ No = 2, Id = 1, Name = "B", Status = 1} ,
new Item{ No = 3, Id = 1, Name = "C", Status = 1} ,
new Item{ No = 4, Id = 1, Name = "D", Status = 1} ,
new Item{ No = 5, Id = 2, Name = "E", Status = 1} ,
new Item{ No = 6, Id = 2, Name = "F", Status = 1} ,
new Item{ No = 7, Id = 2, Name = "G", Status = 1} ,
};
var result = items
.GroupBy(groupedItems => groupedItems.Id)
.SelectMany(i => items
.Where(innerItem => innerItem.Id == i.Key && innerItem.No == i.Max(ii => ii.No))
.Select(innerItem => innerItem)
);
foreach (var item in result)
Console.WriteLine("Max item of Id {0} : No = {1}, Name = {2}, Status = {3}", item.Id, item.No, item.Name, item.Status);
}
private class Item
{
public Int32 No { get; set; }
public Int32 Id { get; set; }
public String Name { get; set; }
public Int32 Status { get; set; }
}
出力:
Max item of Id 1 : No = 4, Name = D, Status = 1
Max item of Id 2 : No = 7, Name = G, Status = 1
別:
items.GroupBy(groupedItems => groupedItems.Id)
.Select(g => g.OrderByDescending(x => x.No).First())