【问题标题】:How to create Lists of List in the following context [duplicate]如何在以下上下文中创建列表列表 [重复]
【发布时间】:2016-07-23 11:52:31
【问题描述】:
public class Person
{
public int Id{get;set;}
public string Name{get;set;}
}
public class Test
{

private readonly IPersonDataService _personDataService;
public Test(IPersonDataService personDataService)
{
_personDataService=personDataService;
}

List<Person> persons=_personDataService.GetList();
List<List<Person>> personLists=new <List<Person>>();
}

想象persons 列表包含 50 个项目。我希望能够从 persons 中提取 5 个列表,每个列表包含 10 个项目,并将每个列表添加到 personLists。 最后,personLists 应该有 5 个项目,每个项目是一个包含 10 个项目的列表。

【问题讨论】:

    标签: c# list ienumerable


    【解决方案1】:

    假设列表 people 总是有 50 个项目,你可以这样做:

    for (int listCount = 0; listCount < 5; listCount++) {
     List<Person> newList = new List<Person>();
     personLists.add(newList);
     for (int innerCount = 0; innerCount < 10; innerCount++) {
      newList.add(persons.next());
     }
    }
    

    请记住,这是未经测试的,但总体概念应该是合理的。

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多