Hooked on LINQ

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

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »

Except Operator

Modified: 2011/12/03 23:35 by admin - 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 Except operator produces the set difference between two sequences. It will only return elements in the first sequence that don't appear in the second. You can optionally provide your own equality comparison function.

Edit

Method Signatures

// 1 - Returns only elements that appear in one of the sequences. public static IEnumerable Except( this IEnumerable first, IEnumerable second)

// New in January CTP // 2 - Returns only elements that appear in one of the sequences. // A custom function is used to determine equality. public static IEnumerable Except( this IEnumerable first, IEnumerable second, IEqualityComparer 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.
Add all the distinct second elements to a list (distinct element list)
Iterate the first sequence.
If the current element is NOT in the distinct element list (as evaluated equal using EqualityComparer<TSource>.Default),
Return the current element (pick up from here when the next element is requested).

Overload 2
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.
Add all the distinct second elements to a list (distinct element list)
Iterate the first sequence.
If the current element is NOT in the distinct element list (as evaluated by the comparer function provided for equality evaluation),
Return the current element (pick up from here when the next element is requested).

Edit

Loop Count

The entire second sequence will be looped.
The first sequence follows: This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.

Edit

Code Samples

This sample shows that the except operator will only return those elements in the first sequence that are not in the second sequence.


public static void ExceptTest() { int">"> first = new int[ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int">"> second = new int[ { 4, 5, 6, 10 };

var q = first.Except(second);

foreach (var num in q) Console.WriteLine(num);

Console.ReadLine(); }

0
1
2
3
7
8
9

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 omissions 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 unintentional errors - please use care. Other websites by this author: Focused Objective, Geek Speak Decoded.