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 Query Expression sample uses ToDictionary to make a new array based on condition. |
This Query Expression sample uses ToDictionary to make a new array based on condition. |
static void Sample_ToDictionary_Linq_Conditional() { int[] numbers = { 1, 2, 3, 4 }; var result = from n in numbers.ToDictionary(k => k, v => (v % 2) == 1 ? "Odd" : "Even") select n; Debug.WriteLine("Numbers labeled Odd and Even in dictionary:"); foreach (var dic in result) Debug.WriteLine("Value {0} is {1}", dic.Key, dic.Value); }Output:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even
Private Shared Sub Sample_ToDictionary_Linq_Conditional() Dim numbers As Integer() = {1, 2, 3, 4} Dim result = From n In numbers.ToDictionary(Function(k) k, Function(v) If((v Mod 2) = 1, "Odd", "Even")) Select n Debug.WriteLine("Numbers labeled Odd and Even in dictionary:") For Each dic In result Debug.WriteLine("Value {0} is {1}", dic.Key, dic.Value) Next End SubOutput:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even
let Sample_ToDictionary_Linq_Conditional() = let numbers = [|1; 2; 3; 4|] let result = query { for n in numbers.ToDictionary((fun k -> k), fun v -> if v % 2 = 1 then "Odd" else "Even") do select n } Debug.WriteLine(sprintf "Numbers labeled Odd and Even in dictionary:") for dic in result do Debug.WriteLine(sprintf "Value %d is %s" dic.Key dic.Value)Output:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even
static void Sample_ToDictionary_Lambda_Conditional() { int[] numbers = { 1, 2, 3, 4 }; var result = numbers.ToDictionary(k => k, v => (v % 2) == 1 ? "Odd" : "Even"); Debug.WriteLine("Numbers labeled Odd and Even in dictionary:"); foreach (var dic in result) Debug.WriteLine("Value {0} is {1}", dic.Key, dic.Value); }Output:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even
Private Shared Sub Sample_ToDictionary_Lambda_Conditional() Dim numbers As Integer() = {1, 2, 3, 4} Dim result = numbers.ToDictionary(Function(k) k, Function(v) If((v Mod 2) = 1, "Odd", "Even")) Debug.WriteLine("Numbers labeled Odd and Even in dictionary:") For Each dic In result Debug.WriteLine("Value {0} is {1}", dic.Key, dic.Value) Next End SubOutput:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even
let Sample_ToDictionary_Lambda_Conditional() = let numbers = [|1; 2; 3; 4|] let result = numbers.ToDictionary((fun k -> k), fun v -> if v % 2 = 1 then "Odd" else "Even") Debug.WriteLine(sprintf "Numbers labeled Odd and Even in dictionary:") for dic in result do Debug.WriteLine(sprintf "Value %d is %s" dic.Key dic.Value)Output:
Numbers labeled Odd and Even in dictionary: Value 1 is Odd Value 2 is Even Value 3 is Odd Value 4 is Even