我有一个相当长的 linq 查询,一切正常..但是在最后的连接中,我在一个有日志的表上做一个内部连接,日志返回超过 50 条记录,我只想要最新的记录..
举个例子
var tst = from w in context.storage
join p in context.products on w.id equals p.wid
join l in context.logger on p.id equals l.pid
select new
{
storageid = w.id,
productid = p.id
productname = p.name
bought = l.when
};
所以快速解释一下会发生什么,每个产品都存储在一个存储中心,并且在该产品被购买时有一个日志,如果它被购买了 100 次,那么记录器中就有 100 条记录。
所以目前它返回 productid = 5 的 50 条记录......为什么......因为它被购买了 50 次但我只想要 1 条记录,因此我只想要来自记录器的最新日期时间。
有人可以帮忙吗?我有点卡住了。
最佳答案
使用 result.Distinct(x => x.Prop)
仅获取唯一条目
使用 result.Max(x => x.Prop)
获取最新日期,使用 Min()
获取最早日期。
关于c# - 从当前返回具有日期时间的每条记录的大型 linq 查询中获取 "latest"日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11027968/