[英]Get Row with Min Date in LINQ?
I've got a table with a date/time in it and I'd like to find the entire row with the minimum date. 我有一个带有日期/时间的表,我想找到带有最小日期的整行。 See this example:
请参阅以下示例:
ID First Last ADateTime Processed
1 Joe Smith 09/06/2013 04:00 AM True
1 Joe Smith 09/06/2013 02:00 AM False
2 Jack Jones
3 John Jack 09/05/2013 06:00 AM True
3 John Jack 09/26/2013 02:00 AM False
What I would want from a query is to get the following: 我希望从查询中获得以下内容:
ID First Last ADateTime Processed
1 Joe Smith 09/06/2013 02:00 AM False
2 Jack Jones
3 John Jack 09/05/2013 06:00 AM True
I have a LINQ statement that comes close: 我有一个接近的LINQ语句:
var uniqueGuys = from d in tests.AsEnumerable()
group d by d.ID into g
let f = g.FirstOrDefault()
where f != null
select new
{
f.ID,
f.First,
f.Last,
ADateTime = g.Min(c => c.DateTime),
f.Processed
};
This gives me each of the 3 IDs with the minimum date correctly, but the rest of the information is incorrect. 这给我3个ID中的每个ID的最小日期正确,但是其余信息不正确。 For example, for ID 1, I get Joe Smith having a date of 09/06/201 2:00 AM, which is right, but it shows processed as True instead of the false that's attached to that row.
例如,对于ID 1,我得到的Joe Smith的日期为09/06/201 2:00 AM,这是对的,但是它显示为True,而不是附加到该行的false。 It looks like it just grabs the minimum date/time but then the first record it can find for the other stuff.
看起来它只是获取了最小的日期/时间,但随后它可以找到其他内容的第一条记录。 Is there a way to get a full row with the minimum ADateTime?
有没有办法用最少的ADateTime来获得整行?
You can do orderby and then select the first. 您可以执行orderby,然后选择第一个。
as in: 如:
var uniqueGuys = from d in from d in tests.AsEnumerable()
group d by d.ID into g
select g.OrderBy(p => p.DateTime).First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.