Linq学习笔记(1.5)——group、Distinct、Union、Concat、Intersect、Except
最近学习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)
{
用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
select new {Lang=studentsgroup.Key,member=studentsgroup};
foreach (var g in students1)
{
Response.Write(string.Format("<div class='result'>Language:{0}</div>",g.Lang));
foreach (var student in g.member)
{
Response.Write(string.Format("<div class='result'>——{0}</div>", student.Name));
}
}
#endregion
用Distinct将过滤相同项#region 用Distinct将过滤相同项
Response.Write("<hr>用Distinct将过滤相同项:<br>");
var Languages = (from student in StudentList
select student.Language)
.Distinct();
foreach (var l in Languages)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用Union连接不同集合体#region 用Union连接不同集合体
Response.Write("<hr>用Union连接不同集合体:<br>");
string[] stringArray = { "Csharp", "Javascript"};
var Languages2 = (from student in StudentList
select student.Language)
.Union(stringArray);
//Union会自动过滤各集合体中的相同项,无需Distinct
foreach (var l in Languages2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用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)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用Intersect取不同集合体的相同项(交集)#region 用Intersect取不同集合体的相同项(交集)
Response.Write("<hr>用Intersect取不同集合体的相同项(交集):<br>");
string[] Book1Array1 = { "Csharp", "Javascript","FrameWork","Use Compute","WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
var Books = Book1Array1.Intersect(Book1Array2);
foreach (var book in Books)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
用Except从集合体中排出和另一个集合体相同的项#region 用Except从集合体中排出和另一个集合体相同的项
Response.Write("<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
var Books2 = Book1Array1.Except(Book1Array2);
foreach (var book in Books2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#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)
{
用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
select new {Lang=studentsgroup.Key,member=studentsgroup};
foreach (var g in students1)
{
Response.Write(string.Format("<div class='result'>Language:{0}</div>",g.Lang));
foreach (var student in g.member)
{
Response.Write(string.Format("<div class='result'>——{0}</div>", student.Name));
}
}
#endregion
用Distinct将过滤相同项#region 用Distinct将过滤相同项
Response.Write("<hr>用Distinct将过滤相同项:<br>");
var Languages = (from student in StudentList
select student.Language)
.Distinct();
foreach (var l in Languages)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用Union连接不同集合体#region 用Union连接不同集合体
Response.Write("<hr>用Union连接不同集合体:<br>");
string[] stringArray = { "Csharp", "Javascript"};
var Languages2 = (from student in StudentList
select student.Language)
.Union(stringArray);
//Union会自动过滤各集合体中的相同项,无需Distinct
foreach (var l in Languages2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用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)
{
Response.Write(string.Format("<span class='result'>{0}</span>", l));
}
#endregion
用Intersect取不同集合体的相同项(交集)#region 用Intersect取不同集合体的相同项(交集)
Response.Write("<hr>用Intersect取不同集合体的相同项(交集):<br>");
string[] Book1Array1 = { "Csharp", "Javascript","FrameWork","Use Compute","WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
var Books = Book1Array1.Intersect(Book1Array2);
foreach (var book in Books)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
用Except从集合体中排出和另一个集合体相同的项#region 用Except从集合体中排出和另一个集合体相同的项
Response.Write("<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
var Books2 = Book1Array1.Except(Book1Array2);
foreach (var book in Books2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#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
结果显示