使用 LINQ 根据条件将集合拆分为多个部分?
我想实现类似于这个的东西。但我不知道我可以以什么方式使用该解决方案。
我的实体具有这些属性
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可能的 LINQ 答案,但我确信存在更有效的答案:
Here's one possible LINQ answer, though I'm sure a more efficient one exists: