使用 LINQ 獲取列表的一部分

拉爾夫·傑森·卡巴萊斯

說,我有一個清單,我想得到它的一部分。假設我有 1000 個從 0 到 999 的數據。然後我想從“index1”到“index2”。

樣本數據是:

[0] = 100
[1] = 1520
....
[900] = 8975
....
[998] = 10
[999] = 4875

比如說我想從索引 900 到索引 998 獲取值。所以值返回應該是:

[0] = 8975
.....
[998] = 10

如何在 LINQ 中做到這一點?

蘭特納

您可以使用skip,並take

List<int> list= new List<int>;
list.Skip(900).Take(100);

https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/skip-and-take

或者,您可以使用該GetRange方法

List<int> list= new List<int>;
list.GetRange(900, 100); // Retrieves 100 items starting with index #900

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.getrange?redirectedfrom=MSDN&view=net-6.0#System_Collections_Generic_List_1_GetRange_System_Int32_System_Int32 _

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章