/**/
////
///以下是Group,Distinct,Union,Concat,Intersect,Except的例子和解释
/////
class Program
{
DB#region DB
private static 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="Chinese"},
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
static void Main(string[] args)
{
List<Student> studentList = GetStudents();
//Group(studentList);
//Distinct(studentList);
//Union(studentList);
//Concat(studentList);
Intersect(studentList);
//Except();
}
Group,Distinct,Union,Concat,Intersect,Except#region Group,Distinct,Union,Concat,Intersect,Except
private static void Group<T>(T obj)
{
UsingGroupToCollectGrouping(obj);
//<hr>用group将集合体分组:<br>
//<div class='result'>Language:Chinese</div>
//<div class='result'>——YOUNG</div>
//<div class='result'>——JUNE</div>
//<div class='result'>——BEN</div>
//<div class='result'>Language:Scotland</div>
//<div class='result'>——JESSIE</div>
//<div class='result'>——BRUCE</div>
//<div class='result'>Language:English</div>
//<div class='result'>——KELLY</div>
//<div class='result'>Language:Italy</div>
//<div class='result'>——ADRIAN</div>
//<div class='result'>Language:Germany</div>
//<div class='result'>——BRANT</div>
}
private static void UsingGroupToCollectGrouping<T>(T obj)
{
Console.WriteLine("<hr>用group将集合体分组:<br>");
var stu = from student in RevealChangeType(obj)
group student by student.Language into studentsgroup
select new { lang = studentsgroup.Key, member = studentsgroup };
foreach (var g in stu)
{
Console.WriteLine(string.Format("<div class='result'>Language:{0}</div>", g.lang));
foreach (var student in g.member)
{
Console.WriteLine(string.Format("<div class='result'>——{0}</div>", student.Name));
}
}
}
private static void Distinct<T>(T obj)
{
UsingDistinctFilterSame(obj);
//<hr>用Distinct将过滤相同项:<br>
//<span class='result'>Chinese</span>
//<span class='result'>Scotland</span>
//<span class='result'>English</span>
//<span class='result'>Italy</span>
//<span class='result'>Germany</span>
}
private static void UsingDistinctFilterSame<T>(T obj)
{
Console.WriteLine("<hr>用Distinct将过滤相同项:<br>");
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Distinct();
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Union<T>(T obj)
{
Console.WriteLine("<hr>用Union连接不同集合体:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis", "Chinese" };
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Union(stringArray);
//Union会自动过滤各集合体中的相同项,无需Distinct
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Concat<T>(T obj)
{
Console.WriteLine("<hr>用Concat连接不同集合体:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis", "Chinese" };
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Concat(stringArray);
//Concat不会自动过滤各集合体中的相同项
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Intersect<T>(T obj)
{
Console.WriteLine("<hr>用Intersect取不同集合体的相同项(交集):<br>");
string[] Book1Array1 = { "Csharp", "Javascript", "FrameWork", "Use Compute", "WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
//var books = Book1Array1.Intersect(Book1Array1);
//<hr>用Intersect取不同集合体的相同项(交集):<br>
//<span class='result'>Csharp</span>
//<span class='result'>Javascript</span>
//<span class='result'>FrameWork</span>
//<span class='result'>Use Compute</span>
//<span class='result'>WebDesign</span>
var books = Book1Array .Intersect(Book1Array2);
//<hr>用Intersect取不同集合体的相同项(交集):<br>
//<span class='result'>Use Compute</span>
//<span class='result'>WebDesign</span>;
foreach (var book in books)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", book));
}
}
private static void Except()
{
string[] Book1Array1 = { "Csharp", "Javascript", "FrameWork", "Use Compute", "WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
Console.WriteLine("<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
var Books2 = Book1Array1.Except(Book1Array2);
foreach (var book in Books2)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", book));
}
}
#endregion
private static IEnumerable<Student> RevealChangeType<T>(T obj)
{
IEnumerable<Student> student = obj as IEnumerable<Student>;
return student;
}
}
sealed class Student
{
public int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string language;
public string Language
{
get { return language; }
set { language = value; }
}
}
///以下是Group,Distinct,Union,Concat,Intersect,Except的例子和解释
/////
class Program
{
DB#region DB
private static 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="Chinese"},
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
static void Main(string[] args)
{
List<Student> studentList = GetStudents();
//Group(studentList);
//Distinct(studentList);
//Union(studentList);
//Concat(studentList);
Intersect(studentList);
//Except();
}
Group,Distinct,Union,Concat,Intersect,Except#region Group,Distinct,Union,Concat,Intersect,Except
private static void Group<T>(T obj)
{
UsingGroupToCollectGrouping(obj);
//<hr>用group将集合体分组:<br>
//<div class='result'>Language:Chinese</div>
//<div class='result'>——YOUNG</div>
//<div class='result'>——JUNE</div>
//<div class='result'>——BEN</div>
//<div class='result'>Language:Scotland</div>
//<div class='result'>——JESSIE</div>
//<div class='result'>——BRUCE</div>
//<div class='result'>Language:English</div>
//<div class='result'>——KELLY</div>
//<div class='result'>Language:Italy</div>
//<div class='result'>——ADRIAN</div>
//<div class='result'>Language:Germany</div>
//<div class='result'>——BRANT</div>
}
private static void UsingGroupToCollectGrouping<T>(T obj)
{
Console.WriteLine("<hr>用group将集合体分组:<br>");
var stu = from student in RevealChangeType(obj)
group student by student.Language into studentsgroup
select new { lang = studentsgroup.Key, member = studentsgroup };
foreach (var g in stu)
{
Console.WriteLine(string.Format("<div class='result'>Language:{0}</div>", g.lang));
foreach (var student in g.member)
{
Console.WriteLine(string.Format("<div class='result'>——{0}</div>", student.Name));
}
}
}
private static void Distinct<T>(T obj)
{
UsingDistinctFilterSame(obj);
//<hr>用Distinct将过滤相同项:<br>
//<span class='result'>Chinese</span>
//<span class='result'>Scotland</span>
//<span class='result'>English</span>
//<span class='result'>Italy</span>
//<span class='result'>Germany</span>
}
private static void UsingDistinctFilterSame<T>(T obj)
{
Console.WriteLine("<hr>用Distinct将过滤相同项:<br>");
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Distinct();
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Union<T>(T obj)
{
Console.WriteLine("<hr>用Union连接不同集合体:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis", "Chinese" };
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Union(stringArray);
//Union会自动过滤各集合体中的相同项,无需Distinct
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Concat<T>(T obj)
{
Console.WriteLine("<hr>用Concat连接不同集合体:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis", "Chinese" };
var lang = (from stu in RevealChangeType(obj)
select stu.Language).Concat(stringArray);
//Concat不会自动过滤各集合体中的相同项
foreach (var l in lang)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", l));
}
}
private static void Intersect<T>(T obj)
{
Console.WriteLine("<hr>用Intersect取不同集合体的相同项(交集):<br>");
string[] Book1Array1 = { "Csharp", "Javascript", "FrameWork", "Use Compute", "WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
//var books = Book1Array1.Intersect(Book1Array1);
//<hr>用Intersect取不同集合体的相同项(交集):<br>
//<span class='result'>Csharp</span>
//<span class='result'>Javascript</span>
//<span class='result'>FrameWork</span>
//<span class='result'>Use Compute</span>
//<span class='result'>WebDesign</span>
var books = Book1Array .Intersect(Book1Array2);
//<hr>用Intersect取不同集合体的相同项(交集):<br>
//<span class='result'>Use Compute</span>
//<span class='result'>WebDesign</span>;
foreach (var book in books)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", book));
}
}
private static void Except()
{
string[] Book1Array1 = { "Csharp", "Javascript", "FrameWork", "Use Compute", "WebDesign" };
string[] Book1Array2 = { "Chanese", "English", "Use Compute", "Business", "WebDesign" };
Console.WriteLine("<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
var Books2 = Book1Array1.Except(Book1Array2);
foreach (var book in Books2)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", book));
}
}
#endregion
private static IEnumerable<Student> RevealChangeType<T>(T obj)
{
IEnumerable<Student> student = obj as IEnumerable<Student>;
return student;
}
}
sealed class Student
{
public int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string language;
public string Language
{
get { return language; }
set { language = value; }
}
}