Hooked on LINQ

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

Quick Search

Advanced Search »

ToLookup Operator

Modified: 2007/01/25 01:33 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 ToLookup operator creates a Lookup from a sequence.

Edit

Method Signatures

// 1 - Create a Lookup of TSource elements (one key to many records) from the source.
public static Lookup<TKey, TSource> ToLookup<TSource, TKey>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector)
 
// 2 - Create a Lookup of TSource elements (one key to many records) from the source,
// using a custom comparer.
public static Lookup<TKey, TSource> ToLookup<TSource, TKey>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    IEqualityComparer<TKey> comparer)
 
// 3 - Create a Lookup of projected elements (one key to many records) from the source.
public static Lookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    Func<TSource, TElement> elementSelector)
 
// 4 - Create a Lookup of projected elements (one key to many records) from the source,
// using a custom comparer.
public static Lookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    Func<TSource, TElement> elementSelector, 
    IEqualityComparer<TKey> comparer)



Edit

Exceptions

Throws an ArgumentNullException if source or keySelector or elementSelector are null.


Edit

Pseudo-code

Overload 1
Set comparer to EqualityComparer<TSource>.Default).
Set element selector the be the TSource instance.
Call and return the result of Overload 4 passing in source, keySelector, element selector and comparer.


Overload 2
Set element selector the be the TSource instance.
Call and return the result of Overload 4 passing in source, keySelector, element selector and comparer.


Overload 3
Set comparer to EqualityComparer<TSource>.Default).
Call and return the result of Overload 4 passing in source, keySelector, elementSelector and comparer.


Overload 4
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
If elementSelector is null, throw an ArgumentNullException.
Create a new Lookup<TSource, TElement>(comparer) instance.
Iterate the source sequence.
Find the key value returned by keySelector(this element).
If this key does not exist in the lookup collection (as determined by using the custom comparer - comparer),
Create a grouping of elementSelector(current element) elements for this key.
Return the Lookup instance.


Edit

Loop Count

1. The source sequence is enumerated to build the groupings.


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.