(转)LINQ查询操作符之DistinctUnionConcatIntersectExcept

Posted mingsonzheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)LINQ查询操作符之DistinctUnionConcatIntersectExcept相关的知识,希望对你有一定的参考价值。

介绍

    ·Distinct - 过滤集合中的相同项;延迟
    ·Union - 连接不同集合,自动过滤相同项;延迟
    ·Concat - 连接不同集合,不会自动过滤相同项;延迟
    ·Intersect - 获取不同集合的相同项(交集);延迟
    ·Except - 从某集合中删除其与另一个集合中相同的项;延迟
    ·Skip - 跳过集合的前n个元素;延迟
    ·Take - 获取集合的前n个元素;延迟
    ·SkipWhile - 直到某一条件成立就停止跳过;延迟
    ·TakeWhile - 直到某一条件成立就停止获取;延迟
    ·Single - 根据表达式返回集合中的某一元素;不延迟
    ·SingleOrDefault - 根据表达式返回集合中的某一元素(如果没有则返回默认值);不延迟
    ·Reverse - 对集合反向排序;延迟
    ·SelectMany - Select选择(一对多);延迟
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.htmlControls; 
using System.Xml.Linq; 
 
using System.Collections.Generic; 
using DAL; 
 
public partial class LINQ_Summary2 : System.Web.UI.Page 
{ 
        NorthwindDataContext _ctx = new NorthwindDataContext(); 
        string[] _ary = null; 
 
        protected void Page_Load(object sender, EventArgs e) 
        { 
                _ary = new string[] { "asp.net", "csharp", "xhtml", "css", "javascript", "wcf", "wpf",    
                        "asp.net", "csharp", "xhtml", "css", "javascript",    
                        "silverlight", "linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs" }; 
 
                // Distinct - 过滤集合中的相同项;延迟 
                Summary_Distinct(); 
 
                // Union - 连接不同集合,自动过滤相同项;延迟 
                Summary_Union(); 
 
                // Concat - 连接不同集合,不会自动过滤相同项;延迟 
                Summary_Concat(); 
 
                // Intersect - 获取不同集合的相同项(交集);延迟 
                Summary_Intersect(); 
 
                // Except - 从某集合中删除其与另一个集合中相同的项;延迟 
                Summary_Except(); 
 
                // Skip - 跳过集合的前n个元素;延迟 
                // Take - 获取集合的前n个元素;延迟 
                Summary_Skip_Take(); 
 
                // SkipWhile - 直到某一条件成立就停止跳过;延迟 
                // TakeWhile - 直到某一条件成立就停止获取;延迟 
                Summary_SkipWhile_TakeWhile(); 
 
                // Single - 根据表达式返回集合中的某一元素;不延迟 
                // SingleOrDefault - 根据表达式返回集合中的某一元素(如果没有则返回默认值);不延迟 
                Summary_Single_SingleOrDefault(); 
 
                // Reverse - 对集合反向排序;延迟 
                Summary_Reverse(); 
 
                // SelectMany - Select选择(一对多);延迟 
                Summary_SelectMany(); 
        } 
}
/// <summary> 
/// Distinct - 过滤集合中的相同项;延迟 
/// </summary> 
void Summary_Distinct() 
{ 
        var ary = (from a in _ary 
                             select a).Distinct(); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp.net
csharp
xhtml
css
javascript
wcf
wpf
silverlight
linq
wf
sqlserver
asp.net ajax
ssis
ssas
ssrs
/// <summary> 
/// Union - 连接不同集合,自动过滤相同项;延迟 
/// </summary> 
void Summary_Union() 
{ 
        var ary = (from a in _ary 
                             select a).Take(3).Union((from a in _ary 
                                                                                select a).Take(6)); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp.net
csharp
xhtml
css
javascript
wcf
/// <summary> 
/// Concat - 连接不同集合,不会自动过滤相同项;延迟 
/// </summary> 
void Summary_Concat() 
{ 
        var ary = (from a in _ary 
                             select a).Take(3).Concat((from a in _ary 
                                                                                 select a).Take(6)); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp.net
csharp
xhtml
asp.net
csharp
xhtml
css
javascript
wcf
 /// <summary> 
/// Intersect - 获取不同集合的相同项(交集);延迟 
/// </summary> 
void Summary_Intersect() 
{ 
        var ary = (from a in _ary 
                             select a).Take(3).Intersect((from a in _ary 
                                                                                        select a).Skip(1).Take(3)); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
csharp
xhtml
/// <summary> 
/// Except - 从某集合中删除其与另一个集合中相同的项;延迟 
/// </summary> 
void Summary_Except() 
{ 
        var ary = (from a in _ary 
                             select a).Take(3).Except((from a in _ary 
                                                                                 select a).Skip(1).Take(3)); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp.net
/// <summary> 
/// Skip - 跳过集合的前n个元素;延迟 
/// Take - 获取集合的前n个元素;延迟 
/// </summary> 
void Summary_Skip_Take() 
{ 
        var ary = (from a in _ary 
                             select a).Skip(2).Take(3); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
xhtml
css
javascript
/// <summary> 
/// SkipWhile - 直到某一条件成立就停止跳过;延迟 
/// TakeWhile - 直到某一条件成立就停止获取;延迟 
/// </summary> 
void Summary_SkipWhile_TakeWhile() 
{ 
        var ary = (from a in _ary 
                             select a).SkipWhile(s => s.Length < 8).TakeWhile(s => s.Length > 2); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
javascript
wcf
wpf
asp.net
csharp
xhtml
css
javascript
silverlight
linq
/// <summary> 
/// Single - 根据表达式返回集合中的某一元素;不延迟 
/// SingleOrDefault - 根据表达式返回集合中的某一元素(如果没有则返回默认值);不延迟 
/// </summary> 
void Summary_Single_SingleOrDefault() 
{ 
        string s = (from a in _ary 
                                select a).Single(a => a == "silverlight"); 
        // string s = (from a in _ary 
        //                         select a).SingleOrDefault(a => a == "xxx"); 
        // s == null 

        result.InnerHtml += s + "<br />"; 
        result.InnerHtml += "<br />"; 
}
运行结果
silverlight
/// <summary> 
/// Reverse - 对集合反向排序;延迟 
/// </summary> 
void Summary_Reverse() 
{ 
        var ary = (from a in _ary 
                             orderby a.Length ascending 
                             select a).Reverse(); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp.net ajax
silverlight
javascript
javascript
sqlserver
asp.net
asp.net
csharp
csharp
xhtml
xhtml
ssrs
ssas
ssis
linq
css
wpf
wcf
css
wf
/// <summary> 
/// SelectMany - Select选择(一对多);延迟 
/// </summary> 
void Summary_SelectMany() 
{ 
        var ary = (from a in _ary 
                             where a.Contains(".") 
                             select a).SelectMany(a => a.Split(‘.‘)); 

        foreach (string s in ary) 
        { 
                result.InnerHtml += s + "<br />"; 
        } 
        result.InnerHtml += "<br />"; 
}
运行结果
asp
net
asp
net
asp
net ajax

原文链接:
http://www.cnblogs.com/qiuweiguo/archive/2011/08/03/2126613.html


以上是关于(转)LINQ查询操作符之DistinctUnionConcatIntersectExcept的主要内容,如果未能解决你的问题,请参考以下文章

LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)

[转]走进 LINQ 的世界

(转)Linq-DataSet

LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun

LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun

LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun