LINQ GROUP BY和获得最新值 [英] LINQ group by and getting latest value
本文介绍了LINQ GROUP BY和获得最新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这样的结构:
class SomeObject
Public Name as String
Public Created as Date
...
end class
我有一个列表(的SomeObject),这对每一个名字而不同时期的多个条目。我想选择每个名称最新(最大的创值)的对象。
I have a List(of SomeObject), which has multiple entries for each name with different times. I'd like to select the newest (largest Created value) object for each Name.
由于:
Name Created
A 2010-04-16 *
A 2010-04-15
B 2010-04-12
B 2010-04-13 *
C 2010-04-16 *
我想去捡与*的对象,他们身边。
I'd like to pick the objects with the * beside them.
使用LINQ这可能吗?
Is this possible using LINQ?
推荐答案
有:
var maxObjects =
from o in myList
group o by o.Name into g
select new { Name = g.Key, Created = g.Max(o => o.Created) };
请参阅 101 LINQ的样本来看看的LINQ如何使用。
See 101 Linq samples to see how Linq can be used.
这篇关于LINQ GROUP BY和获得最新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文