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
ToDictionary: Converts collection into a Dictionary with Key and Value.
ToDictionary: Converts collection into a Dictionary with Key and Value. |
ToDictionary: Converts collection into a Dictionary with Key and Value. |
This Lambda Expression sample inserts custom values into a dictionary. |
This Lambda Expression sample inserts custom values into a dictionary. |
class English2German { public string EnglishSalute { get; set; } public string GermanSalute { get; set; } } static void Sample_ToDictionary_Linq_Simple() { English2German[] english2German = { new English2German { EnglishSalute = "Good morning", GermanSalute = "Guten Morgen" }, new English2German { EnglishSalute = "Good day", GermanSalute = "Guten Tag" }, new English2German { EnglishSalute = "Good evening", GermanSalute = "Guten Abend" }, }; var result = from e in english2German.ToDictionary(k => k.EnglishSalute, v => v.GermanSalute) select e; Debug.WriteLine("Values put into dictionary:"); foreach (KeyValuePair<string, string> dic in result) Debug.WriteLine(String.Format("English salute {0} is {1} in German", dic.Key, dic.Value)); }Output:
Values put into dictionary: English salute Good morning is Guten Morgen in German English salute Good day is Guten Tag in German English salute Good evening is Guten Abend in German
Private Class English2German Public Property EnglishSalute() As String Public Property GermanSalute() As String End Class Private Shared Sub Sample_ToDictionary_Linq_Simple() Dim english2German As English2German() = { New English2German() With { _ .EnglishSalute = "Good morning", _ .GermanSalute = "Guten Morgen" _ }, New English2German() With { _ .EnglishSalute = "Good day", _ .GermanSalute = "Guten Tag" _ }, New English2German() With { _ .EnglishSalute = "Good evening", _ .GermanSalute = "Guten Abend" _ }} Dim result = From e In english2German.ToDictionary(Function(k) k.EnglishSalute, Function(v) v.GermanSalute) Select e Debug.WriteLine("Values inserted into dictionary:") For Each dic As KeyValuePair(Of String, String) In result Debug.WriteLine([String].Format("English salute {0} is {1} in German", dic.Key, dic.Value)) Next End SubOutput:
Values inserted into dictionary: English salute Good morning is Guten Morgen in German English salute Good day is Guten Tag in German English salute Good evening is Guten Abend in German
class English2German { public string EnglishSalute { get; set; } public string GermanSalute { get; set; } } static void Sample_ToDictionary_Lambda_Simple() { English2German[] english2German = { new English2German { EnglishSalute = "Good morning", GermanSalute = "Guten Morgen" }, new English2German { EnglishSalute = "Good day", GermanSalute = "Guten Tag" }, new English2German { EnglishSalute = "Good evening", GermanSalute = "Guten Abend" }, }; var result = english2German.ToDictionary(k => k.EnglishSalute, v => v.GermanSalute); Debug.WriteLine("Values inserted into dictionary:"); foreach (KeyValuePair<string, string> dic in result) Debug.WriteLine(String.Format("English salute {0} is {1} in German", dic.Key, dic.Value)); }Output:
Values put into dictionary: English salute Good morning is Guten Morgen in German English salute Good day is Guten Tag in German English salute Good evening is Guten Abend in German
Private Class English2German Public Property EnglishSalute() As String Public Property GermanSalute() As String End Class Private Shared Sub Sample_ToDictionary_Lambda_Simple() Dim english2German As English2German() = { New English2German() With { _ .EnglishSalute = "Good morning", _ .GermanSalute = "Guten Morgen" _ }, New English2German() With { _ .EnglishSalute = "Good day", _ .GermanSalute = "Guten Tag" _ }, New English2German() With { _ .EnglishSalute = "Good evening", _ .GermanSalute = "Guten Abend" _ }} Dim result = english2German.ToDictionary(Function(k) k.EnglishSalute, Function(v) v.GermanSalute) Debug.WriteLine("Values inserted into dictionary:") For Each dic As KeyValuePair(Of String, String) In result Debug.WriteLine([String].Format("English salute {0} is {1} in German", dic.Key, dic.Value)) Next End SubOutput:
Values inserted into dictionary: English salute Good morning is Guten Morgen in German English salute Good day is Guten Tag in German English salute Good evening is Guten Abend in German
let Sample_ToDictionary_Lambda_Simple() = let english2German = [| new English2German("Good morning", "Guten Morgen"); new English2German("Good day", "Guten Tag"); new English2German("Good evening", "Guten Abend") |] let result = english2German.ToDictionary((fun k -> k.EnglishSalute), fun (v:English2German) -> v.GermanSalute) Debug.WriteLine(sprintf "Values inserted into dictionary:") for dic in result do Debug.WriteLine(sprintf "English salute %s is %s in German" dic.Key dic.Value)Output:
Values inserted into dictionary: English salute Good morning is Guten Morgen in German English salute Good day is Guten Tag in German English salute Good evening is Guten Abend in German