Поделиться через


Queryable.Intersect Метод

Определение

Создает набор пересечения двух последовательностей.

Перегрузки

Имя Описание
Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>)

Создает набор пересечения двух последовательностей с помощью сравнения значений по умолчанию.

Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

Создает набор пересечения двух последовательностей с помощью указанного IEqualityComparer<T> для сравнения значений.

Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>)

Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs

Создает набор пересечения двух последовательностей с помощью сравнения значений по умолчанию.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Intersect(System::Linq::IQueryable<TSource> ^ source1, System::Collections::Generic::IEnumerable<TSource> ^ source2);
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2);
static member Intersect : System.Linq.IQueryable<'Source> * seq<'Source> -> System.Linq.IQueryable<'Source>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Intersect : System.Linq.IQueryable<'Source> * seq<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Intersect(Of TSource) (source1 As IQueryable(Of TSource), source2 As IEnumerable(Of TSource)) As IQueryable(Of TSource)

Параметры типа

TSource

Тип элементов входных последовательностей.

Параметры

source1
IQueryable<TSource>

Последовательность, в которой возвращаются различные элементы, которые также отображаются source2 .

source2
IEnumerable<TSource>

Последовательность, элементы которой также отображаются в первой последовательности, возвращаются.

Возвращаемое значение

IQueryable<TSource>

Последовательность, содержащая пересечение набора двух последовательностей.

Атрибуты

Исключения

source1 или source2 есть null.

Примеры

В следующем примере кода показано, как использовать Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) для возврата элементов, которые отображаются в каждой из двух последовательностей.

int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };

// Get the numbers that occur in both arrays (id1 and id2).
IEnumerable<int> both = id1.AsQueryable().Intersect(id2);

foreach (int id in both)
    Console.WriteLine(id);

/*
    This code produces the following output:

    26
    30
*/
Dim id1() As Integer = {44, 26, 92, 30, 71, 38}
Dim id2() As Integer = {39, 59, 83, 47, 26, 4, 30}

' Get the numbers that occur in both arrays (id1 and id2).
Dim both As IEnumerable(Of Integer) = id1.AsQueryable().Intersect(id2)

Dim output As New System.Text.StringBuilder
For Each id As Integer In both
    output.AppendLine(id)
Next

' Display the output.
MsgBox(output.ToString())

' This code produces the following output:

' 26
' 30

Комментарии

Метод Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) создает объект MethodCallExpression , представляющий Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) себя как созданный универсальный метод. Затем он передает MethodCallExpressionCreateQuery<TElement>(Expression) метод IQueryProvider метода, представленного Provider свойством source1 параметра.

Поведение запроса, возникающее в результате выполнения дерева выражений, представляющего вызов Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) , зависит от реализации типа source1 параметра. Ожидаемое поведение заключается в том, что возвращаются все элементы, source1source2 в которых также находятся.

Применяется к

Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs
Исходный код:
Queryable.cs

Создает набор пересечения двух последовательностей с помощью указанного IEqualityComparer<T> для сравнения значений.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Intersect(System::Linq::IQueryable<TSource> ^ source1, System::Collections::Generic::IEnumerable<TSource> ^ source2, System::Collections::Generic::IEqualityComparer<TSource> ^ comparer);
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2, System.Collections.Generic.IEqualityComparer<TSource> comparer);
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2, System.Collections.Generic.IEqualityComparer<TSource>? comparer);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2, System.Collections.Generic.IEqualityComparer<TSource>? comparer);
static member Intersect : System.Linq.IQueryable<'Source> * seq<'Source> * System.Collections.Generic.IEqualityComparer<'Source> -> System.Linq.IQueryable<'Source>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Intersect : System.Linq.IQueryable<'Source> * seq<'Source> * System.Collections.Generic.IEqualityComparer<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Intersect(Of TSource) (source1 As IQueryable(Of TSource), source2 As IEnumerable(Of TSource), comparer As IEqualityComparer(Of TSource)) As IQueryable(Of TSource)

Параметры типа

TSource

Тип элементов входных последовательностей.

Параметры

source1
IQueryable<TSource>

Возвращается отдельный IQueryable<T> элемент, который также отображается source2 .

source2
IEnumerable<TSource>

Возвращаются IEnumerable<T> отдельные элементы, которые также отображаются в первой последовательности.

comparer
IEqualityComparer<TSource>

Значение для сравнения значений IEqualityComparer<T> .

Возвращаемое значение

IQueryable<TSource>

Объект, IQueryable<T> содержащий набор пересечения двух последовательностей.

Атрибуты

Исключения

source1 или source2 есть null.

Комментарии

Метод Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) создает объект MethodCallExpression , представляющий Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) себя как созданный универсальный метод. Затем он передает MethodCallExpressionCreateQuery<TElement>(Expression) метод IQueryProvider метода, представленного Provider свойством source1 параметра.

Поведение запроса, возникающее в результате выполнения дерева выражений, представляющего вызов Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) , зависит от реализации типа source1 параметра. Ожидаемое поведение заключается в том, что возвращаются все элементы, source1source2 в которых также находятся. Параметр comparer используется для сравнения элементов.

Применяется к