C# 如何从 List<T> 跳过(m).take(n)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2106915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to skip(m).take(n) from a List<T>?
提问by Mike108
Given:
鉴于:
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
How do I implement the following code?
如何实现以下代码?
var list2 = list.skip(2).take(5);
采纳答案by Esteban Araya
Your sample code will work as long as you include System.Linq
in your using statements (and fix your method names .Skip(2)
and .Take(5)
).
只要您包含System.Linq
在 using 语句中(并修复您的方法名称.Skip(2)
和.Take(5)
),您的示例代码就会起作用。
The reason your code did not work out of the box is that .Skip
and .Take
are extension methods (as opposed to methods defined in the List class) found in the 'System.Linq' namespace.
你的代码没有盒子的锻炼的原因是,.Skip
和.Take
为扩展方法(而不是List类中定义的方法)在“System.Linq的”命名空间中。
回答by Bala
Have a look at the samples in the following link and its more easy to go with
查看以下链接中的示例,它更容易使用
LINQ 101 Sample
LINQ 101 示例