LINQ-使用distinct和orderby编写查询
我对林克是个新手 假设我有下表:LINQ-使用distinct和orderby编写查询,linq,sql-order-by,distinct,Linq,Sql Order By,Distinct,我对林克是个新手 假设我有下表: Incident ID DeviceID Time Info 1 1 5/2/2009 d 2 2 5/3/2009 c 3 2 5/4/2009 b 4 1 5/5/2009 a 在LINQ中,我如何编写一个查询来查找最新和不同的(设备ID上)事件集?我想要的结果是: ID DeviceID Time Info 3
Incident
ID DeviceID Time Info
1 1 5/2/2009 d
2 2 5/3/2009 c
3 2 5/4/2009 b
4 1 5/5/2009 a
在LINQ中,我如何编写一个查询来查找最新和不同的(设备ID上)事件集?我想要的结果是:
ID DeviceID Time Info
3 2 5/4/2009 b
4 1 5/5/2009 a
您是否必须创建一个IEqualityComparer来执行此操作?您可以通过以下方式获取每个设备的最新事件(这是我理解您的问题的方式):
是的,正是我想要的。谢谢
var query =
incidents.GroupBy(incident => incident.DeviceID)
.Select(g => g.OrderByDescending(incident => incident.Time).First())
.OrderBy(i => i.Time); // only add if you need results sorted
int filterDeviceID = 10;
var incidents = (from incident in incidentlist
where incident.DeviceID == filterDeviceID
select incident).Distinct().OrderBy( x => x.Time);