我想實現類似於this的操作。但我不知道我能以何種方式使用該解決方案。根據LINQ的條件將一個集合拆分成幾部分?
我的實體具有這些屬性
CustomerName
Date
SortOrder
我有這個實體的完整列表。我想要做的是,組列表<所有這些項目>具有連續SortOrder的和同日期和同客戶名稱
例輸入
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}
}
};
UPDATE
非LINQ解決方案也足夠了。
這將極大地幫助,如果你能有一個小數據樣本,以及你希望這些數據到底是什麼看起來像一旦它被分組。 – diceguyd30 2010-10-26 12:17:51
@ diceguyd30!我已更新問題 – IsmailS 2010-10-26 12:37:28
爲什麼現在放置在與其他客戶「Abc」訂單相同的列表中的兩個底行(CustomerName爲「Abc」和DateTime爲Today)? – 2010-10-26 12:42:34