1、Skip操作符
Skip
操作符用于从输入序列中跳过指定数量的元素,返回由序列中剩余的元素所组成的新序列。
例如,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<People> pList = new List<People>();
People p1 = new People(1, "C", 4);
People p2 = new People(1, "Java", 7);
People p3 = new People(1, "Python", 11);
People p4 = new People(1, "Linux", 15);
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);
pList.Add(p4);
IEnumerable<People> newList = pList.Skip(2); //跳过前两个
foreach (var item in newList)
{
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
}
public class People
{
public People(int id, string name, int age)
{
this.Id = id;
this.Name = name;
this.Age = age;
}
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
}
2、SkipWhile操作符
SkipWhile
操作符用于从输入序列中跳过满足一定条件指定数量的元素,与TakeWhile
操作符类似。如果条件满足就一直跳过,直到不满足后 就取剩下的所有元素(后面的不会再判断)。
例如,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<People> pList = new List<People>();
People p1 = new People(1, "C", 6);
People p2 = new People(1, "C#", 11);
People p3 = new People(1, "Java", 8);
People p4 = new People(1, "Python", 15);
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);
pList.Add(p4);
IEnumerable<People> newList = pList.SkipWhile(p => p.Age < 11);
foreach (var item in newList)
{
Console.WriteLine(item.Name);
}
Console.WriteLine();
IEnumerable<People> newList1 = pList.SkipWhile((p, i) => p.Age<12 && i < 2);
foreach (People p in newList1)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
}
public class People
{
public People(int id, string name, int age)
{
this.Id = id;
this.Name = name;
this.Age = age;
}
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
}