最近学习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)
按条件选择后根据指定对象的某个属性排序选择结果#region 按条件选择后根据指定对象的某个属性排序选择结果
Response.Write("<hr>按条件选择后根据指定对象的某个属性排序选择结果:<br>");
List<Student> StudentList = GetStudents();
var students1 = from student in StudentList
where student.Age>20
orderby student.Age descending //或ascending,默认为ascending
select student;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in students1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, student.Name));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
多个排序条件#region 多个排序条件
Response.Write("<hr>多个排序条件:<br>");
var students2 = from student in StudentList
orderby student.Language,student.Age descending
select student;
foreach (var student in students2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}; Language:{2}</div>", student.Age, student.Name, student.Language));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
通过Reverse()对查询结果反向排序#region 通过Reverse()对查询结果反向排序
Response.Write("<hr>通过Reverse()对查询结果反向排序:<br>");
var students3 = (from student in StudentList
orderby student.Age
select student)
.Reverse();
foreach (var student in students3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, student.Name));
}
#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 18:33
Young.Jiang
阅读(
4832)
评论()
编辑
收藏
举报