Linq学习笔记(1.3)——Take、Skip
最近学习Linq时练习的一些Demo
using#region using
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
从集合中取固定数量的元素#region 从集合中取固定数量的元素
Response.Write("<hr>从集合中取固定数量的元素:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = stringArray.Take(3);
foreach (var book in books){
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
从select结果中取固定数量的元素#region 从select结果中取固定数量的元素
Response.Write("<hr>从select结果中取固定数量的元素:<br>");
List<Student> StudentList = GetStudents();
var students1 = (from n in StudentList
where n.Language == "Chinese"
select new {n.Name,n.Age }).Take(2);
foreach (var student in students1)
{
Response.Write(string.Format("<div class='result'>name:{0}; age:{1}</div>", student.Name, student.Age));
}
#endregion
只查询合适对象集合的某个属性#region 只查询合适对象集合的某个属性
Response.Write("<hr>跳过集合的几个元素后取剩余所有元素:<br>");
var books1 = stringArray.Skip(3);
foreach (var book in books1)
{
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
取满足指定条件元素#region 取满足指定条件元素
Response.Write("<hr>取满足指定条件元素:<br>");
var students2 = StudentList.TakeWhile(student => student.Age > 20);
//相当与where和select的结合
foreach (var student in students2)
{
Response.Write(string.Format("<div class='result'>{0}</div>", student.Name));
}
#endregion
将满足指定条件元素过滤后选择剩余的所有元素#region 将满足指定条件元素过滤后选择剩余的所有元素
Response.Write("<hr>将满足指定条件元素过滤后选择剩余的所有元素:<br>");
var students3 = StudentList.SkipWhile(student => student.Age > 20);
//相当与where和select的结合后反选
foreach (var student in students3)
{
Response.Write(string.Format("<div class='result'>{0}</div>", student.Name));
}
#endregion
}
构造一个学生集合体#region 构造一个学生集合体
private List<Student> GetStudents()
{
List<Student> students = new List<Student> {
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
new Student{ Name="KELLY", Age=18, Language="English"},
new Student{ Name="JUNE", Age=20, Language="English"},
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
new Student{ Name="BRANT", Age=30, Language="Germany"},
new Student{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
}
#endregion
}
学生类#region 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#endregion
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
从集合中取固定数量的元素#region 从集合中取固定数量的元素
Response.Write("<hr>从集合中取固定数量的元素:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = stringArray.Take(3);
foreach (var book in books){
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
从select结果中取固定数量的元素#region 从select结果中取固定数量的元素
Response.Write("<hr>从select结果中取固定数量的元素:<br>");
List<Student> StudentList = GetStudents();
var students1 = (from n in StudentList
where n.Language == "Chinese"
select new {n.Name,n.Age }).Take(2);
foreach (var student in students1)
{
Response.Write(string.Format("<div class='result'>name:{0}; age:{1}</div>", student.Name, student.Age));
}
#endregion
只查询合适对象集合的某个属性#region 只查询合适对象集合的某个属性
Response.Write("<hr>跳过集合的几个元素后取剩余所有元素:<br>");
var books1 = stringArray.Skip(3);
foreach (var book in books1)
{
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
取满足指定条件元素#region 取满足指定条件元素
Response.Write("<hr>取满足指定条件元素:<br>");
var students2 = StudentList.TakeWhile(student => student.Age > 20);
//相当与where和select的结合
foreach (var student in students2)
{
Response.Write(string.Format("<div class='result'>{0}</div>", student.Name));
}
#endregion
将满足指定条件元素过滤后选择剩余的所有元素#region 将满足指定条件元素过滤后选择剩余的所有元素
Response.Write("<hr>将满足指定条件元素过滤后选择剩余的所有元素:<br>");
var students3 = StudentList.SkipWhile(student => student.Age > 20);
//相当与where和select的结合后反选
foreach (var student in students3)
{
Response.Write(string.Format("<div class='result'>{0}</div>", student.Name));
}
#endregion
}
构造一个学生集合体#region 构造一个学生集合体
private List<Student> GetStudents()
{
List<Student> students = new List<Student> {
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
new Student{ Name="KELLY", Age=18, Language="English"},
new Student{ Name="JUNE", Age=20, Language="English"},
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
new Student{ Name="BRANT", Age=30, Language="Germany"},
new Student{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
}
#endregion
}
学生类#region 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#endregion
结果显示: