我有一个具有相同ObjectId的对象列表,我想返回具有最新DateOrdered的对象。这是一个很好的方法吗?
var LatestObject = (from o in Context.Objects
where p.ObjectId == ObjectId
select p.DateOrdered)
.ToList()
.Max();
答案 0 :(得分:5)
不,它只会显示最新日期。不是您的对象,而是可以尝试:
Context.Objects.Where(p => p.ObjectId == ObjectId)
.OrderByDescending(p => p.DateOrdered)
.First();
答案 1 :(得分:2)
你可以试试这个:
var result = Context.Objects.Where(p => p.ObjectId == ObjectId).OrderByDescending(x => x.DateOrdered).FirstOrDefault();