2.4.12 Skip、SkipWhile、Take、TakeWhile
- 评论:0
- 浏览:493
- RSS:2
2.3.12 Skip、SkipWhile、Take、TakeWhile
l Skip:跳过集合的指定前几个元素,此函数延时加载。
例:
protected void btnSkip1_Click(object sender, EventArgs e)
{
int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7 };
GridView1.DataSource = ints.Skip(3);
GridView1.DataBind();
}
执行结果为:返回4,5,6,7,跳过1,2,3。
protected void btnSkip2_Click(object sender, EventArgs e)
{
ProvinceCityDataContext dal = new ProvinceCityDataContext();
GridView1.DataSource = dal.Cities.Skip(10);
GridView1.DataBind();
}
执行结果为:跳过前10条记录,返回以后的记录。
l SkipWhile:只有当满足条件的时候,才开始返回元素,而不管元素以后的值是否满足。(仅Linq to Object支持此函数),此函数延时加载。
例:
protected void btnSkipWhile_Click(object sender, EventArgs e)
{
int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7 };
GridView1.DataSource = ints.SkipWhile(s =>s>5).ToList();
GridView1.DataBind();
}
l Take:从集合中下标为0开始截取指定条数的元素,其余则忽略,此函数延时加载。
例:
protected void btnTake_Click(object sender, EventArgs e)
{
int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7 };
GridView1.DataSource = ints.Take(3);
GridView1.DataBind();
}
以上执行结果为返回:1,2,3。其余的则忽略。
protected void btnTake2_Click(object sender, EventArgs e)
{
ProvinceCityDataContext dal = new ProvinceCityDataContext();
GridView1.DataSource = dal.Cities.OrderBy(s=>s.ID).Take(10);
GridView1.DataBind();
}
以上执行结果为返回按照ID排序的城市集合中的头10条记录。
l TakeWhile:只有当满足条件的时候,才开始截取元素,而不管元素以后的值是否满足。(仅Linq to Object支持此函数),此函数延时加载。
例:
protected void btnTakeWhile_Click(object sender, EventArgs e)
{
int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7 };
GridView1.DataSource = ints.TakeWhile(s=>s==4);
GridView1.DataBind();
}