Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Intersect Operator

Modified: 2007/01/16 20:43 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Intersect operator produces the set intersection of two sequences.

Edit

Method Signatures

// 1 - Find the intersection between two sequences.
public static IEnumerable<TSource> Intersect<TSource>(
    this IEnumerable<TSource> first, 
    IEnumerable<TSource> second)
 
// New in January CTP
// 2 - Find the intersection between two sequences using custom comparer
public static IEnumerable<TSource> Intersect<TSource>(
    this IEnumerable<TSource> first, 
    IEnumerable<TSource> second, 
    IEqualityComparer<TSource> comparer)



Edit

Exceptions

Throws an ArgumentNullException if first or second are null.

Edit

Pseudo-code

Overload 1
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.
Iterate the second sequence.
Add the current element to a list (list1).
Iterate the first sequence.
If this element is in the list (list1) as determined using the EqualityComparer<TSource>.Default; comparer function.
Return the current element. (pick up from here when the next element is requested).

Overload 2 - custom comparer
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.
If comparer is null, default to EqualityComparer<TSource>.Default;
Iterate the second sequence.
Add the current element to a list (list1).
Iterate the first sequence.
If this element is in the list (list1) as determined using the specified comparer function.
Return the current element. (pick up from here when the next element is requested).

Edit

Loop Count

1 loop through second sequence initially, then 1 loop through the first sequence as an iterator. This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.

Edit

Code Samples

TODO: Needs code sample.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.