15

这可能吗?

例如,如果我有

List<Item> myList = new List<Item>;

//added 100 of Items to myList

//then I want to grab items at indices 50 - 60

List<Item> myNewList = myList.?

如果不循环通过 myList,我该怎么做?谢谢你。

4

4 回答 4

41

有一个方法可以检索这些项目,List.GetRange。 http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

List<Item> myList = new List<Item>;
myList.GetRange(50, 10); // Retrieves 10 items starting with index #50
于 2012-07-19T18:15:39.380 回答
8
var myNewList  = myList.Skip(50).Take(10);
于 2012-07-19T18:15:29.883 回答
1

http://msdn.microsoft.com/en-us/library/bb503062.aspx

http://msdn.microsoft.com/en-us/library/bb357513.aspx

myList.Skip(50).Take(10)
于 2012-07-19T18:16:26.470 回答
1

关于什么:

        List<string> myList = new List<string>();

        //added 100 of Items to myList
        for (int i=0; i<100; ++i)
        {
            myList.Add("blablabla");
        }

        //then I want to grab items at indices 50 - 60
        // Note: 50 - 60 inclusivly are actually 11 items
        List<string> myNewList = myList.GetRange(50, 11); 
于 2012-07-19T18:17:06.330 回答