步步为营VS 2008 + .NET 3.5(6) - LINQ查询操作符之Distinct、Union、Concat、Intersect、Except

[索引页]
[源码下载]


步步为营VS 2008 + .NET 3.5(6) - LINQ查询操作符之Distinct、Union、Concat、Intersect、Except、Skip、Take、SkipWhile、TakeWhile、Single、SingleOrDefault、Reverse、SelectMany


作者:webabcd


介绍
    ・Distinct - 过滤集合中的相同项;延迟
    ・Union - 连接不同集合,自动过滤相同项;延迟
    ・Concat - 连接不同集合,不会自动过滤相同项;延迟
    ・Intersect - 获取不同集合的相同项(交集);延迟
    ・Except - 从某集合中删除其与另一个集合中相同的项;延迟
    ・Skip - 跳过集合的前n个元素;延迟
    ・Take - 获取集合的前n个元素;延迟
    ・SkipWhile - 直到某一条件成立就停止跳过;延迟
    ・TakeWhile - 直到某一条件成立就停止获取;延迟
    ・Single - 根据表达式返回集合中的某一元素;不延迟
    ・SingleOrDefault - 根据表达式返回集合中的某一元素(如果没有则返回默认值);不延迟
    ・Reverse - 对集合反向排序;延迟
    ・SelectMany - Select选择(一对多);延迟


示例
Summary2.aspx.cs
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();
        }
}
 
Distinct - 过滤集合中的相同项;延迟
/// <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

Union - 连接不同集合,自动过滤相同项;延迟
/// <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

Concat - 连接不同集合,不会自动过滤相同项;延迟
/// <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

Intersect - 获取不同集合的相同项(交集);延迟
/// <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

Except - 从某集合中删除其与另一个集合中相同的项;延迟
/// <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

Skip - 跳过集合的前n个元素;延迟
Take - 获取集合的前n个元素;延迟
/// <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

SkipWhile - 直到某一条件成立就停止跳过;延迟
TakeWhile - 直到某一条件成立就停止获取;延迟
/// <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

Single - 根据表达式返回集合中的某一元素;不延迟
SingleOrDefault - 根据表达式返回集合中的某一元素(如果没有则返回默认值);不延迟
/// <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

Reverse - 对集合反向排序;延迟
/// <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

SelectMany - Select选择(一对多);延迟
/// <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


OK
[源码下载]

你可能感兴趣的:(UNION,distinct,concat,操作符,except)