使用LINQ分手列表&LT; T&GT;到大量的表&LT的; T&GT; n个长度是多少? [英] Use linq to break up list<t> into lots of list<t> of n length?
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/1349491/how-can-i-split-an-ienumerablestring-into-groups-of-ienumerablestring">How我可以分裂一个IEnumerable&LT;字符串&GT;到了IEnumerable&LT组;串&GT;
我有一个,我想打入10个组列表。
如果我有一个对象
名单,其中,人物&GT; allPendingPersons
这是长度为m
有没有在LINQ一种优雅的方式allPendingPersons分解为一个或多个列表对象,它们有一个多达10人?
VAR组= allPendingPersons.Select((P,指数)=&gt;新建{对,指数})
.GroupBy(一个=&GT; a.index / 10);
如果你想处理 IGrouping&LT;,&GT;
。如果您正在寻找名单>回来,你可以尝试
VAR listOfLists = allPendingPersons.Select((P,指数)=&gt;新建{对,指数})
.GroupBy(一个=&GT; a.index / 10)
。选择((GRP =&GT; grp.Select(G =&GT; GP).ToList()))
.ToList();
Possible Duplicate:
How can I split an IEnumerable<String> into groups of IEnumerable<string>
I have a list that I would like to break into groups of 10.
If I have an object
List<Person> allPendingPersons
that is of length m.
Is there an elegant way in LINQ to break up allPendingPersons into one or more List objects that all have a up to 10 Persons?
var groups = allPendingPersons.Select((p, index) => new {p,index})
.GroupBy(a =>a.index/10 );
if you want to process IGrouping<,>
. If you are looking for List> back you could try
var listOfLists = allPendingPersons.Select((p, index) => new {p, index})
.GroupBy(a => a.index/10)
.Select((grp => grp.Select(g => g.p).ToList()))
.ToList();
这篇关于使用LINQ分手列表&LT; T&GT;到大量的表&LT的; T&GT; n个长度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!