Linq 入门系列 [Group,Distinct,Union,Concat,Intersect,Except篇]

ExpandedBlockStart.gif ContractedBlock.gif   /**/ ////
InBlock.gif    
///以下是Group,Distinct,Union,Concat,Intersect,Except的例子和解释
ExpandedBlockEnd.gif    
/////

None.gif      class  Program
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DB#region DB
InBlock.gif
InBlock.gif        
private static List<Student> GetStudents()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            List
<Student> students = new List<Student> dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="YOUNG", Age=25, Language="Chinese"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JESSIE", Age=21, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="KELLY", Age=18, Language="English"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JUNE", Age=20, Language="Chinese"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="ADRIAN", Age=22, Language="Italy"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRUCE", Age=17, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRANT", Age=30, Language="Germany"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BEN", Age=25, Language="Chinese"}
ExpandedSubBlockEnd.gif             }
;
InBlock.gif            
return students;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List
<Student> studentList = GetStudents();
InBlock.gif
InBlock.gif            
//Group(studentList);
InBlock.gif
InBlock.gif            
//Distinct(studentList);
InBlock.gif
InBlock.gif            
//Union(studentList);
InBlock.gif
InBlock.gif            
//Concat(studentList);
InBlock.gif

InBlock.gif            Intersect(studentList);
InBlock.gif
InBlock.gif            
//Except();
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Group,Distinct,Union,Concat,Intersect,Except#region Group,Distinct,Union,Concat,Intersect,Except
InBlock.gif
InBlock.gif        
private static void Group<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UsingGroupToCollectGrouping(obj);
InBlock.gif
InBlock.gif            
//<hr>用group将集合体分组:<br>
InBlock.gif            
//<div class='result'>Language:Chinese</div>
InBlock.gif            
//<div class='result'>——YOUNG</div>
InBlock.gif            
//<div class='result'>——JUNE</div>
InBlock.gif            
//<div class='result'>——BEN</div>
InBlock.gif            
//<div class='result'>Language:Scotland</div>
InBlock.gif            
//<div class='result'>——JESSIE</div>
InBlock.gif            
//<div class='result'>——BRUCE</div>
InBlock.gif            
//<div class='result'>Language:English</div>
InBlock.gif            
//<div class='result'>——KELLY</div>
InBlock.gif            
//<div class='result'>Language:Italy</div>
InBlock.gif            
//<div class='result'>——ADRIAN</div>
InBlock.gif            
//<div class='result'>Language:Germany</div>
InBlock.gif            
//<div class='result'>——BRANT</div>
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void UsingGroupToCollectGrouping<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>用group将集合体分组:<br>");
InBlock.gif            var stu 
= from student in RevealChangeType(obj)
InBlock.gif                      group student by student.Language into studentsgroup
ExpandedSubBlockStart.gifContractedSubBlock.gif                      select 
new dot.gif{ lang = studentsgroup.Key, member = studentsgroup };
InBlock.gif
InBlock.gif            
foreach (var g in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>Language:{0}</div>", g.lang));
InBlock.gif                
foreach (var student in g.member)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
string.Format("<div class='result'>——{0}</div>", student.Name));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Distinct<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UsingDistinctFilterSame(obj);
InBlock.gif
InBlock.gif            
//<hr>用Distinct将过滤相同项:<br>
InBlock.gif            
//<span class='result'>Chinese</span>
InBlock.gif            
//<span class='result'>Scotland</span>
InBlock.gif            
//<span class='result'>English</span>
InBlock.gif            
//<span class='result'>Italy</span>
InBlock.gif            
//<span class='result'>Germany</span>
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void UsingDistinctFilterSame<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>用Distinct将过滤相同项:<br>");
InBlock.gif            var lang 
= (from stu in RevealChangeType(obj)
InBlock.gif                        select stu.Language).Distinct();
InBlock.gif
InBlock.gif            
foreach (var l in lang)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", l));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Union<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>用Union连接不同集合体:<br>");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis""Chinese" };
InBlock.gif
InBlock.gif            var lang 
= (from stu in RevealChangeType(obj)
InBlock.gif                        select stu.Language).Union(stringArray);
InBlock.gif
InBlock.gif            
//Union会自动过滤各集合体中的相同项,无需Distinct
InBlock.gif
            foreach (var l in lang)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", l));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Concat<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>用Concat连接不同集合体:<br>");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis""Chinese" };
InBlock.gif
InBlock.gif            var lang 
= (from stu in RevealChangeType(obj)
InBlock.gif                        select stu.Language).Concat(stringArray);
InBlock.gif
InBlock.gif            
//Concat不会自动过滤各集合体中的相同项
InBlock.gif
            foreach (var l in lang)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", l));
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Intersect<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>用Intersect取不同集合体的相同项(交集):<br>");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] Book1Array1 = dot.gif"Csharp""Javascript""FrameWork""Use Compute""WebDesign" };
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] Book1Array2 = dot.gif"Chanese""English""Use Compute""Business""WebDesign" };
InBlock.gif
InBlock.gif            
//var books = Book1Array1.Intersect(Book1Array1);
InBlock.gif            
//<hr>用Intersect取不同集合体的相同项(交集):<br>
InBlock.gif            
//<span class='result'>Csharp</span>
InBlock.gif            
//<span class='result'>Javascript</span>
InBlock.gif            
//<span class='result'>FrameWork</span>
InBlock.gif            
//<span class='result'>Use Compute</span>
InBlock.gif            
//<span class='result'>WebDesign</span>
InBlock.gif

InBlock.gif            var books 
= Book1Array .Intersect(Book1Array2);
InBlock.gif            
//<hr>用Intersect取不同集合体的相同项(交集):<br>
InBlock.gif            
//<span class='result'>Use Compute</span>
InBlock.gif            
//<span class='result'>WebDesign</span>;
InBlock.gif

InBlock.gif            
foreach (var book in books)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", book));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Except()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] Book1Array1 = dot.gif"Csharp""Javascript""FrameWork""Use Compute""WebDesign" };
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] Book1Array2 = dot.gif"Chanese""English""Use Compute""Business""WebDesign" };
InBlock.gif
InBlock.gif            Console.WriteLine(
"<hr>用Except从集合体中排出和另一个集合体相同的项:<br>");
InBlock.gif            var Books2 
= Book1Array1.Except(Book1Array2);
InBlock.gif            
foreach (var book in Books2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", book));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private static IEnumerable<Student> RevealChangeType<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IEnumerable
<Student> student = obj as IEnumerable<Student>;
InBlock.gif
InBlock.gif            
return student;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
sealed   class  Student
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public int age;
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ age = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string language;
InBlock.gif        
public string Language
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn language; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ language = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值