使用 LINQ 根据条件将集合拆分为多个部分?

发布于 2024-09-28 18:15:59 字数 2022 浏览 1 评论 0原文

我想实现类似于这个的东西。但我不知道我可以以什么方式使用该解决方案。

我的实体具有这些属性

CustomerName
Date
SortOrder

我拥有该实体的完整列表。我想要做的是将 List<> 中的所有这些项目分组为具有连续的 SortOrder 和相同的 Date 以及相同的 CustomerName

输入示例

  var inv = new List<Invoice>(){
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 0},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 1},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 2},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 5},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 6}
  };

输出示例

  var invGrouped = new List<List<Invoice>>
   {
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 0},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 1},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 2}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 5},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 6}

       }
   };

<强>更新
非 LINQ 解决方案也足够了。

I want to achieve something similar to this. But I don't know in what manner I can use that solution.

My entity has these properties

CustomerName
Date
SortOrder

I've whole list of this Entity. What I want to do is, group all those items in List<> which have consecutive SortOrder and same Date and same CustomerName

Example input

  var inv = new List<Invoice>(){
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 0},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 1},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 2},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 5},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 6}
  };

Example output

  var invGrouped = new List<List<Invoice>>
   {
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 0},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 1},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 2}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 5},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 6}

       }
   };

UPDATE
Non-LINQ solution will also suffice.

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

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

发布评论

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

评论(1

孤芳又自赏 2024-10-05 18:15:59

这是一个可能的 LINQ 答案,但我确信存在更有效的答案:

    inv
        .GroupBy(x => new { CustomerName = x.CustomerName, Date = x.Date })
        .SelectMany(x => x
                            .OrderBy(y => y.SortOrder)
                            .Select((y,i) => new { Value = y, Sort = y.SortOrder - i })
                            .GroupBy(y => y.Sort)
                            .Select(y => y.Select(z => z.Value))
        )

Here's one possible LINQ answer, though I'm sure a more efficient one exists:

    inv
        .GroupBy(x => new { CustomerName = x.CustomerName, Date = x.Date })
        .SelectMany(x => x
                            .OrderBy(y => y.SortOrder)
                            .Select((y,i) => new { Value = y, Sort = y.SortOrder - i })
                            .GroupBy(y => y.Sort)
                            .Select(y => y.Select(z => z.Value))
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文