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 (simple)

Lambda Query

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.

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

Share this sample on:

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