{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The ToLookup operator creates a Lookup from a sequence.
EditMethod 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)EditExceptions
Throws an ArgumentNullException if
source or
keySelector or
elementSelector are null.
EditPseudo-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.
EditLoop Count
1. The
source sequence is enumerated to build the groupings.
EditCode Samples
TODO: Needs code sample.
Century 21 Broker Properti Jual Beli Sewa Rumah Indonesia