Linq学习笔记(1.4)——orderby、Reverse()
最近学习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>");
List<Student> StudentList = GetStudents();
var students1 = from student in StudentList
where student.Age>20
orderby student.Age descending //或ascending,默认为ascending
select student;
foreach (var student in students1)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, student.Name));
}
#endregion
多个排序条件#region 多个排序条件
Response.Write("<hr>多个排序条件:<br>");
var students2 = from student in StudentList
orderby student.Language,student.Age descending
select student;
foreach (var student in students2)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}; Language:{2}</div>", student.Age, student.Name, student.Language));
}
#endregion
通过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)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, 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>");
List<Student> StudentList = GetStudents();
var students1 = from student in StudentList
where student.Age>20
orderby student.Age descending //或ascending,默认为ascending
select student;
foreach (var student in students1)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, student.Name));
}
#endregion
多个排序条件#region 多个排序条件
Response.Write("<hr>多个排序条件:<br>");
var students2 = from student in StudentList
orderby student.Language,student.Age descending
select student;
foreach (var student in students2)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}; Language:{2}</div>", student.Age, student.Name, student.Language));
}
#endregion
通过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)
{
Response.Write(string.Format("<div class='result'>age:{0}; name:{1}</div>", student.Age, 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
结果显示