我需要将客户列表(List)分组,其中Customer包含此枚举:
public enum CustomerType
{
type1,
type2,
type3,
type4,
type5
}
按类型和每种类型的项目数(在本例中为50),例如,如果客户包含:
70 type1,
120 type2 and
51 type5
你的答案应该返回一个Dictionary对象列表,如:
Object 1:
50 type1,
50 type2 and
50 type5
Object 2:
20 type1,
50 type2 and
1 type5
Object 3:
50 type2
基本上,Dictionary对象的工作方式类似于包含50个任意类型项的页面。
答案 0 :(得分:1)
这是你在找什么?它会返回IEnumerable<IEnumerable<Customer>>
,而不是List<Dictionary<?,Customer>>
,部分原因是因为我不确定您想要什么作为密钥。
var pageSize=3; // Change to 50
var cust=new[] {
new {CustomerType=1,CustomerName="A"},
new {CustomerType=1,CustomerName="B"},
new {CustomerType=1,CustomerName="C"},
new {CustomerType=1,CustomerName="D"},
new {CustomerType=1,CustomerName="E"},
new {CustomerType=1,CustomerName="F"},
new {CustomerType=1,CustomerName="G"},
new {CustomerType=1,CustomerName="H"},
new {CustomerType=2,CustomerName="I"},
new {CustomerType=2,CustomerName="J"},
new {CustomerType=2,CustomerName="K"},
new {CustomerType=2,CustomerName="L"},
new {CustomerType=2,CustomerName="M"},
new {CustomerType=2,CustomerName="N"},
new {CustomerType=3,CustomerName="O"},
new {CustomerType=3,CustomerName="P"},
new {CustomerType=4,CustomerName="Q"}
};
var max=cust.GroupBy(c=>c.CustomerType).Select(c=>c.Count()).Max();
var dict=Enumerable
.Range(0,(max-1)/pageSize+1)
.Select(page=>
cust.GroupBy(c=>c.CustomerType)
.Select(c=>
c.Skip(page*pageSize)
.Take(pageSize))
.SelectMany(c=>c));
结果: