如何在join和groupby之后进行排序

时间:2014-11-25 20:46:04

标签: c# linq linq-to-entities

如何在join和groupby之后进行排序?如何按字段ps.end订购?

var query =
    from p in _productRepository.Table
    join ps in _productScheduleRepository.Table on p.Id equals ps.ProductId
    where p.Published &&
    !p.Deleted &&
    p.ShowOnHomePage &&
    !ps.IsUsage && ps.Start <= DateTime.Now && ps.End >= DateTime.Now
    group p by p.Id into Grouped
    select Grouped.FirstOrDefault();

1 个答案:

答案 0 :(得分:3)

要订购论坛中的项目,只需致电OrderBy

var query = from p in _productRepository.Table
    join ps in _productScheduleRepository.Table on p.Id equals ps.ProductId
    where ...
    group new{ p, ps } by p.Id into Grouped
    select Grouped.OrderBy(pair => pair.ps.end).FirstOrDefault().p;