最近学习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)
用group将集合体分组#region 用group将集合体分组
Response.Write("<hr>用group将集合体分组:<br>");
List<Student> StudentList = GetStudents();
var students1 = from student in StudentList
group student by student.Language into studentsgroup
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{Lang=studentsgroup.Key,member=studentsgroup};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var g in students1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<div class='result'>Language:{0}</div>",g.Lang));
foreach (var student in g.member)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<div class='result'>——{0}</div>", student.Name));
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Distinct将过滤相同项#region 用Distinct将过滤相同项
Response.Write("<hr>用Distinct将过滤相同项:<br>");
var Languages = (from student in StudentList
select student.Language)
.Distinct();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var l in Languages)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Union连接不同集合体#region 用Union连接不同集合体
Response.Write("<hr>用Union连接不同集合体:<br>");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] stringArray =
{ "Csharp", "Javascript"};
var Languages2 = (from student in StudentList
select student.Language)
.Union(stringArray);
//Union会自动过滤各集合体中的相同项,无需Distinct
foreach (var l in Languages2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Concat连接不同集合体#region 用Concat连接不同集合体
Response.Write("<hr>用Concat连接不同集合体:<br>");
var Languages3 = (from student in StudentList
select student.Language)
.Concat(stringArray);
//Concat不会自动过滤各集合体中的相同项
foreach (var l in Languages3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Intersect取不同集合体的相同项(交集)#region 用Intersect取不同集合体的相同项(交集)
Response.Write("<hr>用Intersect取不同集合体的相同项(交集):<br>");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] Book1Array1 =
{ "Csharp", "Javascript","FrameWork","Use Compute","WebDesign" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] Book1Array2 =
{ "Chanese", "English", "Use Compute", "Business", "WebDesign" };
var Books = Book1Array1.Intersect(Book1Array2);
foreach (var book in Books)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用Except从集合体中排出和另一个集合体相同的项#region 用Except从集合体中排出和另一个集合体相同的项
Response.Write("<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
var Books2 = Book1Array1.Except(Book1Array2);
foreach (var book in Books2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#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/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 19:29
Young.Jiang
阅读(
4924)
评论()
编辑
收藏
举报