private static readonly int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
static void Main()
{
UseTakeMethod();
UseSkipMethod();
UseTakeWhileMethod();
UseSkipWhileMethod();
}
public static void UseTakeMethod()
{
var takeNumbers = numbers.Take(3);
Console.WriteLine("First three numbers");
foreach (var number in takeNumbers)
{
Console.WriteLine("{0}", number);
}
}
public static void UseSkipMethod()
{
var skipNumbers = numbers.Skip(4);
Console.WriteLine("Get all but skip first four element");
foreach (var number in skipNumbers)
{
Console.WriteLine("{0}", number);
}
}
public static void UseTakeWhileMethod()
{
var takeWhileNumbers = numbers.TakeWhile((n, index) => n > index);
Console.WriteLine("Get first element not less than position");
foreach (var number in takeWhileNumbers)
{
Console.WriteLine("{0}", number);
}
}
public static void UseSkipWhileMethod()
{
var skipWhileNumbers = numbers.SkipWhile(n => n > 2);
Console.WriteLine("A specify condition is true that return remaining elements");
foreach (var number in skipWhileNumbers)
{
Console.WriteLine("{0}", number);
}
}LINQ Partitioning Operator
最新推荐文章于 2019-09-21 04:36:48 发布
本文介绍如何利用C#中的Enumerable扩展方法,如Take、Skip、TakeWhile和SkipWhile,对数组进行高效的数据筛选和提取操作。
332

被折叠的 条评论
为什么被折叠?



