Hooked on LINQ

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

Quick Search

Advanced Search »

Union Operator

Modified: 2007/01/25 01:52 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 Union operator produces the set union of two sequences.

Edit

Method Signatures

// 1 - Returns elements which appear in both sequences.
public static IEnumerable<TSource> Union<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
 
// New in January CTP
// 2 - Returns elements which appear in both sequences, using a custom comparer function
public static IEnumerable<TSource> Union<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
 



Edit

Exceptions

Throws an ArgumentNullException if first or second is null.


Edit

Pseudo-code

Overload 1
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.
Create a collection to hold elements we have already returned.
Iterate the source sequence.
If the current element isn't in the already returned list (by using the default comparer EqualityComparer<TElement>.Default.
Add the current element to the already returned list.
Return the current element. Resume execution from this point when the next element is requested.

Overload 2
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.
If comparer is null, set the comparer to EqualityComparer<TElement>.Default.
Iterate the source sequence.
If the current element isn't in the already returned list by using the comparer function.
Add the current element to the already returned list.
Return the current element. Resume execution from this point when the next element is requested.

Edit

Loop count

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.