LINQ In 子句与 Group By 和每组内的前 N ​​条记录

发布于 2024-12-19 22:25:33 字数 552 浏览 2 评论 0 原文

我有如下两堂课。如果我有一个像 List 这样的类 Schedule 的通用列表,我将如何编写 LINQ 查询,以便我可以获得此列表中 SourceId 匹配 1,2,3...X 且 StartTime > 的所有项目。某个日期时间,然后我想返回每个 SourceId 的前 3 个元素(即:按 SourceId 分组)

由于此列表将包含大量记录,我想编写最有效的 LINQ 查询

另外我想要结果的最终形状以List的形式

public class Source
{
    public int SourceId { get; set; }
    public string Name { get; set; }
}

public class Schedule
{
    public int SourceId { get; set; }
    public Source Source { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

I have 2 classes as below. If I had a generic list of class Schedule like List how would I write a LINQ query such that I can get all items within this List where SourceId matches 1,2,3...X and StartTime > some date time and then I want to return top 3 elements for each SourceId (ie: group by SourceId)

Since this List would be containing huge number of records, I want to write the most efficient LINQ query

Also I want the final shape of result to be in the form of List

public class Source
{
    public int SourceId { get; set; }
    public string Name { get; set; }
}

public class Schedule
{
    public int SourceId { get; set; }
    public Source Source { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-12-26 22:25:33

经过几个小时的谷歌搜索后:

var result = r.ScheduleList.
             Where(s => sourceIdIntArray.Contains(s.SourceId) &&
                   s.StartTime >= new DateTime(2011, 09, 20)).
             GroupBy(s => s.SourceId).
             SelectMany(g => g.OrderBy(s => s.StartTime).
                             Take(3)).
             ToList();

after hours of googling:

var result = r.ScheduleList.
             Where(s => sourceIdIntArray.Contains(s.SourceId) &&
                   s.StartTime >= new DateTime(2011, 09, 20)).
             GroupBy(s => s.SourceId).
             SelectMany(g => g.OrderBy(s => s.StartTime).
                             Take(3)).
             ToList();
围归者 2024-12-26 22:25:33

假设:

  • 没有 2 个源具有相同的源 ID,
  • 您希望按 StartTime 排序前 3 个元素
  • 您可以使用 List>作为您的结果(前 3 个按源 ID 分组,仍然以列表形式返回)

    IEnumerable<时间表>时间表;
    日期时间 someDateTime; // 用于比较的日期时间
    IEnumerable;源 ID; // 需要匹配的源 Id
    
    Schedules.OrderBy(x=>x.StartTime)
    .Where(x => x.StartTime > someDateTime)
    .GroupBy(x=>x.Source.SourceId)
    .Where(x=>sourceIds.Contains(x.Key))
    .ToDictionary(x=>x.Key, x=>x.Take(3))
    .ToList()
    

Assuming that:

  • no 2 sources have the same source Id
  • you want the top 3 elements ordered by StartTime
  • you're ok with having a List<KeyValuePair<int,Schedule>> as your result (top 3 grouped by Source Id and still returned as a list)

    IEnumerable<Schedule> schedules;
    DateTime someDateTime; // date time used for comparison
    IEnumerable<int> sourceIds; // required matching source Ids
    
    schedules.OrderBy(x=>x.StartTime)
    .Where(x => x.StartTime > someDateTime)
    .GroupBy(x=>x.Source.SourceId)
    .Where(x=>sourceIds.Contains(x.Key))
    .ToDictionary(x=>x.Key, x=>x.Take(3))
    .ToList()
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文