【问题标题】:Get part of the list using LINQ [duplicate]使用 LINQ 获取列表的一部分 [重复]
【发布时间】:2022-01-06 22:17:43
【问题描述】:

比如说,我有一份清单,我想从中分一杯羹。假设我有 0 到 999 的 1000 个数据。然后我想从“index1”到“index2”。

样本数据是:

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

比如说我想从索引 900 到索引 998 获取值。所以返回的值应该是:

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

如何在 LINQ 中做到这一点?

【问题讨论】:

标签: c# linq


【解决方案1】:

您可以为此使用skiptake

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_

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多