最近学习Linq时练习的一些Demo
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class _Default : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用First()选择查询结果的第一个元素#region 用First()选择查询结果的第一个元素
Response.Write("<hr>用First()选择查询结果的第一个元素<br>");
Student student = (from s in GetStudents()
where s.Language == "Chinese"
select s)
.First();
Response.Write(string.Format("<div class='result'>{0},{1},{2}</div>", student.Name, student.Age, student.Language));
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用First()的Lambda表达式选择查询结果的第一个元素#region 用First()的Lambda表达式选择查询结果的第一个元素
Response.Write("<hr>用First()的Lambda表达式选择查询结果的第一个元素<br>");
Student student2 = GetStudents().First(s => s.Age < 20);
Response.Write(string.Format("<div class='result'>{0},{1},{2}</div>", student2.Name, student2.Age, student2.Language));
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用ElementAt()选择查询结果中指定位置的元素#region 用ElementAt()选择查询结果中指定位置的元素
Response.Write("<hr>用ElementAt()选择查询结果中指定位置的元素<br>");
Student student3 = GetStudents().ElementAt(3);
Response.Write(string.Format("<div class='result'>{0},{1},{2}</div>", student3.Name, student3.Age, student3.Language));
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Any判断查询结果中是否有元素满足条件#region 用Any判断查询结果中是否有元素满足条件
Response.Write("<hr>用Any判断查询结果中是否有元素满足条件<br>");
bool ifany = GetStudents().Any(s=>s.Age>20);
Response.Write(ifany.ToString());
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用all()判断查询结果是否所有元素都满足条件#region 用all()判断查询结果是否所有元素都满足条件
Response.Write("<hr>用all()判断查询结果是否所有元素都满足条件<br>");
bool ifall = GetStudents().All(s => s.Age > 20);
Response.Write(ifall.ToString());
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造一个学生集合体#region 构造一个学生集合体
private List<Student> GetStudents()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<Student> students = new List<Student>
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="YOUNG", Age=25, Language="Chinese"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="JESSIE", Age=21, Language="Scotland"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="KELLY", Age=18, Language="English"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="JUNE", Age=20, Language="English"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="ADRIAN", Age=22, Language="Italy"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BRUCE", Age=17, Language="Scotland"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BRANT", Age=30, Language="Germany"},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
学生类#region 学生类
class Student
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Language
{ get; set; }
}
#endregion
posted @
2007-06-05 22:43
Young.Jiang
阅读(
3583)
评论()
编辑
收藏
举报