Queryable.Concat<TSource> 方法

定義

串連兩個序列。

C#
public static System.Linq.IQueryable<TSource> Concat<TSource> (this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2);

類型參數

TSource

輸入序列之項目的類型。

參數

source1
IQueryable<TSource>

要串連的第一個序列。

source2
IEnumerable<TSource>

要串連到第一個序列的序列。

傳回

IQueryable<TSource>

IQueryable<T>,其中包含兩個輸入序列的串連項目。

例外狀況

source1source2null

範例

下列程式代碼範例示範如何使用 Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 來串連兩個序列。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// This method creates and returns an array of Pet objects.
static Pet[] GetCats()
{
    Pet[] cats = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };
    return cats;
}

// This method creates and returns an array of Pet objects.
static Pet[] GetDogs()
{
    Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
                   new Pet { Name="Snoopy", Age=14 },
                   new Pet { Name="Fido", Age=9 } };
    return dogs;
}

public static void ConcatEx1()
{
    Pet[] cats = GetCats();
    Pet[] dogs = GetDogs();

    // Concatenate a collection of cat names to a
    // collection of dog names by using Concat().
    IEnumerable<string> query =
        cats.AsQueryable()
        .Select(cat => cat.Name)
        .Concat(dogs.Select(dog => dog.Name));

    foreach (string name in query)
        Console.WriteLine(name);
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

備註

方法 Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 會產生 , MethodCallExpression 表示呼叫 Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery<TElement>(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource1

執行表示呼叫 Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source1 。 預期的行為是 中的 source2 專案會串連至 的 source1 元素,以建立新的序列。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0