Aggregation
Aggregate, Average, Count, LongCount, Max, Min, Sum
Conversion
AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup
Element
ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
ElementAt
ElementAtOrDefault
First (simple)
First (conditional)
FirstOrDefault
Last
LastOrDefault (simple)
LastOrDefault (conditional)
Single
SingleOrDefault
Generation
DefaultIfEmpty, Empty, Range, Repeat
Grouping
GroupBy
Join
GroupJoin, Join
Ordering
OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending
OrderBy (simple - numbers)
OrderBy (simple - dates)
OrderBy (simple - objects)
OrderByDescending
Reverse
ThenBy
ThenByDescending
Other
Concat, SequenceEqual, Zip
Partitioning
Skip, SkipWhile, Take, TakeWhile
Projection
Select, SelectMany
Quantifiers
All, Any, Contains
Restriction
Where
Set
Distinct, Except, Intersect, Union
Close
ToLookup: Converts a collection into a Lookup, grouped by key.
ToLookup: Converts a collection into a Lookup, grouped by key. |
ToLookup: Converts a collection into a Lookup, grouped by key. |
This Query Expression sample puts array elements into a one-to-many Lookup, where key equals element length. |
This Query Expression sample puts array elements into a one-to-many Lookup, where key equals element length. |
// A Query Expression cannot be constructed for ToLookup() in C#. // Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for ToLookup() in VB.NET. ' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for ToLookup() in F#. // Consider using a Lambda Expression instead.
static void Sample_ToLookup_Lambda() { string[] words = { "one", "two", "three", "four", "five", "six", "seven" }; var result = words.ToLookup(w => w.Length); for (int i = 1; i <= 5; i++) { Debug.WriteLine(String.Format("Elements with a length of {0}:", i)); foreach (string word in result[i]) Debug.WriteLine(word); } }Output:
Elements with a length of 1: Elements with a length of 2: Elements with a length of 3: one two six Elements with a length of 4: four five Elements with a length of 5: three seven
Private Shared Sub Sample_ToLookup_Lambda() Dim words As String() = {"one", "two", "three", "four", "five", "six", _ "seven"} Dim result = words.ToLookup(Function(w) w.Length) For i As Integer = 1 To 5 Debug.WriteLine([String].Format("Elements with a length of {0}:", i)) For Each word As String In result(i) Debug.WriteLine(word) Next Next End SubOutput:
Elements with a length of 1: Elements with a length of 2: Elements with a length of 3: one two six Elements with a length of 4: four five Elements with a length of 5: three seven
let Sample_ToLookup_Lambda() = let words = [|"one"; "two"; "three"; "four"; "five"; "six"; "seven"|] let result = words.ToLookup(fun w -> w.Length) for i in 1 .. 5 do Debug.WriteLine(sprintf "Elements with a length of %d:" i) for word in result.[i] do Debug.WriteLine(sprintf "%s" word)Output:
Elements with a length of 1: Elements with a length of 2: Elements with a length of 3: one two six Elements with a length of 4: four five Elements with a length of 5: three seven