LINQSamples.com
  • Samples
  • Tutorials
  • About
  • Links
  • Contact
LINQ to Objects

Aggregation

Aggregate, Average, Count, LongCount, Max, Min, Sum

      Aggregate (simple)       Aggregate (seed)       Average       Count       LongCount       Max       Min       Sum

Conversion

AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup

      AsEnumerable       Cast       OfType       ToArray       ToDictionary (simple)       ToDictionary (conditional)       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

      DefaultIfEmpty (simple)       DefaultIfEmpty (default value)       Empty       Range       Repeat

Grouping

GroupBy

      GroupBy

Join

GroupJoin, Join

      GroupJoin (left outer join)       Join (inner join)

Ordering

OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending

      OrderBy (simple - numbers)       OrderBy (simple - dates)       OrderBy (simple - objects)       OrderByDescending       Reverse       ThenBy       ThenByDescending

Other

Concat, SequenceEqual, Zip

      Concat (simple - numbers)       Concat (simple - strings)       SequenceEqual       Zip

Partitioning

Skip, SkipWhile, Take, TakeWhile

      Skip       SkipWhile       Take       TakeWhile

Projection

Select, SelectMany

      Select (simple)       Select (anonymous type)       Select (indexed)       SelectMany (cross join)

Quantifiers

All, Any, Contains

      All       Any       Contains

Restriction

Where

      Where (simple - numbers)       Where (simple - objects)       Where (indexed)

Set

Distinct, Except, Intersect, Union

      Distinct       Except       Intersect       Union
  Close

ToDictionary (cond.)

Lambda Query

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.

  • C#
  • VB.Net
  • F#
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 Sub
Output:
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
  • C#
  • VB.Net
  • F#
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 Sub
Output:
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

Share this sample on:

© 2018 - LINQSamples.com | Terms Of Service  -  Privacy Policy