c# - join和groupby后如何排序

标签 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();

最佳答案

要订购组中的项目,只需调用 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;

关于c# - join和groupby后如何排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27136367/

相关文章:

c# - 根据xml中的信息过滤列表

asp.net-mvc - 如果记录不存在,则在为给定行名称选择整数 ID 时,LINQ to Entities 将返回什么?

c# - ModelStateDictionairy 格式错误的 json Web api

c# - 无法在我的网站上注册用户(PHP、Unity3D)

.net - 带有 RX 扩展的 LINQ

c# - 动态构建从 linq 到实体查询的选择列表

c# - 有没有一种简单的方法来处理 linq-to-entities 查询中的重音符号

c# - Blazor Web assembly 应用程序显示来自服务器的图像

c# - Process.StartTime 访问被拒绝(.Net Core)

c# - 使用 linq 时从 WCF 服务返回什么?