使用LINQ分手列表< T>到大量的表&LT的; T> n个长度是多少?长度、列表、LINQ、GT

2023-09-02 21:10:40 作者:道尽痴情

可能重复:   How我可以分裂一个IEnumerable<字符串>到了IEnumerable&LT组;串>

我有一个,我想打入10个组列表。

如果我有一个对象

 名单,其中,人物> allPendingPersons
 

这是长度为m

有没有在LINQ一种优雅的方式allPendingPersons分解为一个或多个列表对象,它们有一个多达10人?

解决方案

  VAR组= allPendingPersons.Select((P,指数)=>新建{对,指数})
                               .GroupBy(一个=> a.index / 10);
 
第十二节 Lambda linq SQL的相爱相杀 1

如果你想处理 IGrouping<,> 。如果您正在寻找名单>回来,你可以尝试

  VAR listOfLists = allPendingPersons.Select((P,指数)=>新建{对,指数})
    .GroupBy(一个=> a.index / 10)
    。选择((GRP => grp.Select(G => 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();