这可能吗?
例如,如果我有
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的情况下这样做? 谢谢。
答案 0 :(得分:29)
有一种方法可以检索这些项目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
答案 1 :(得分:5)
var myNewList = myList.Skip(50).Take(10);
答案 2 :(得分:1)
http://msdn.microsoft.com/en-us/library/bb503062.aspx
http://msdn.microsoft.com/en-us/library/bb357513.aspx
myList.Skip(50).Take(10)
答案 3 :(得分:0)
怎么样:
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);