Enumerable.Intersect<'TSource> Method (IEnumerable<'TSource>, IEnumerable<'TSource>, IEqualityComparer<'TSource>)
Produces the set intersection of two sequences by using the specified IEqualityComparer<'T> to compare values.
Assembly: System.Core (in System.Core.dll)
static member Intersect<'TSource> : first:IEnumerable<'TSource> * second:IEnumerable<'TSource> * comparer:IEqualityComparer<'TSource> -> IEnumerable<'TSource>
An IEnumerable<'T> whose distinct elements that also appear in second will be returned.
An IEnumerable<'T> whose distinct elements that also appear in the first sequence will be returned.
Return ValueType: System.Collections.Generic.IEnumerable<'TSource>
A sequence that contains the elements that form the set intersection of two sequences.
The type of the elements of the input sequences.
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no other elements.
When the object returned by this method is enumerated, Intersect<'TSource> enumerates first, collecting all distinct elements of that sequence. It then enumerates second, marking those elements that occur in both sequences. Finally, the marked elements are yielded in the order in which they were collected.
If comparer is null, the default equality comparer, Default, is used to compare values.
The following example shows how to implement an equality comparer that can be used in themethod.
After you implement this comparer, you can use sequences of Product objects in the method, as shown in the following example.
Available since 4.5
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1