本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。
譯文
原文
本主題尚未接受評分 - 為這個主題評分

Enumerable.Union<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)

使用預設相等比較值來比較值,以便產生兩個序列的集合等位。

命名空間:  System.Linq
組件:  System.Core (在 System.Core.dll 中)
public static IEnumerable<TSource> Union<TSource>(
	this IEnumerable<TSource> first,
	IEnumerable<TSource> second
)

型別參數

TSource

輸入序列的項目之型別。

參數

first
型別:System.Collections.Generic.IEnumerable<TSource>
IEnumerable<T> ,其獨特項目構成第一個等位集合。
second
型別:System.Collections.Generic.IEnumerable<TSource>
IEnumerable<T> ,其獨特項目構成第二個等位集合。

傳回值

型別:System.Collections.Generic.IEnumerable<TSource>
IEnumerable<T> ,其中包含來自兩個輸入序列的項目,但不包括重複的項目。

使用注意事項

在 Visual Basic 和 C# 中,您可以在任何 IEnumerable<TSource> 型別物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱擴充方法 (Visual Basic)擴充方法 (C# 程式設計手冊)
例外狀況條件
ArgumentNullException

firstsecondnull

這個方法是利用延遲執行所實作。 立即傳回值是一個物件,儲存執行該動作所需的所有資訊。 這個方法所代表的查詢必須等到列舉物件 (透過直接呼叫其 GetEnumerator 方法,或是使用 Visual C# 中的 foreach 或 Visual Basic 中的 For Each) 之後才會執行。

這個方法會將重複項目從傳回集合中排除。 這項行為與 Concat<TSource> 方法不同,後者會傳回輸入序列中的所有項目,包括重複項目。

預設相等比較子 Default 會用來比較實作 IEqualityComparer<T> 泛型介面的型別值。 若要比較自訂資料型別,您必須實作這個介面,並為型別提供您自己的 GetHashCodeEquals 方法。

在列舉此方法所傳回的物件時,Union 會依該順序列舉 firstsecond,並產生尚未產生的每個項目。

下列程式碼範例示範如何使用 Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 來取得兩個整數序列的聯集。


            int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
            int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };

            IEnumerable<int> union = ints1.Union(ints2);

            foreach (int num in union)
            {
                Console.Write("{0} ", num);
            }

            /*
             This code produces the following output:

             5 3 9 7 8 6 4 1 0
            */



如果想比較自訂資料型別的物件序列,您必須在類別中實作 IEqualityComparer<T> 泛型介面。 下列程式碼範例示範如何在自訂資料型別實作這個介面,並提供 GetHashCodeEquals 方法。


public class Product : IEquatable<Product>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(Product other)
    {

        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null.
        int hashProductName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}


實作這個介面後,您就可以如下列範例所示在 Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法中使用 Product 物件的序列。


Product[] store1 = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 } };

Product[] store2 = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };


...


//Get the products from the both arrays
//excluding duplicates.

IEnumerable<Product> union =
  store1.Union(store2);

foreach (var product in union)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:

    apple 9
    orange 4
    lemon 12
*/


.NET Framework

支援版本:4.5、4、3.5

.NET Framework Client Profile

支援版本:4、3.5 SP1

可攜式類別庫

支援版本:可攜式類別庫

適用於 Windows 市集應用程式的 .NET

支援版本:Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)

.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求
本文對您有任何幫助嗎?
(剩餘 1500 個字元)

社群新增項目

新增
© 2013 Microsoft. 著作權所有,並保留一切權利。