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

Join (inner join)

Lambda Query

Join: Joins two collections by a common key value, and is similar to inner join in SQL.

Join: Joins two collections by a common key value, and is similar to inner join in SQL.

This Query Expression sample joins two arrays where elements match in both.

This Query Expression sample joins two arrays where elements match in both.

  • C#
  • VB.Net
  • F#
static void Sample_Join_Linq()
{
    string[] warmCountries = { "Turkey", "Italy", "Spain", "Saudi Arabia", "Etiobia" };
    string[] europeanCountries = { "Denmark", "Germany", "Italy", "Portugal", "Spain" };

    var result = (from w in warmCountries
                  join e in europeanCountries on w equals e
                  select w);

    Debug.WriteLine("Joined countries which are both warm and European using Query Syntax:");
    foreach (var country in result)
        Debug.WriteLine(country);
}
Output:
Joined countries which are both warm and European using Query Syntax:
Italy
Spain
Private Shared Sub Sample_Join_Linq()
    Dim warmCountries As String() = {"Turkey", "Italy", "Spain", "Saudi Arabia", "Etiobia"}
    Dim europeanCountries As String() = {"Denmark", "Germany", "Italy", "Portugal", "Spain"}

    Dim result = From w In warmCountries _
                 Join e In europeanCountries On w Equals e _
                 Select w

    Debug.WriteLine("Joined countries which are both warm and European using Query Syntax:")
    For Each country In result
        Debug.WriteLine(country)
    Next
End Sub
Output:
Joined countries which are both warm and European using Query Syntax:
Italy
Spain
let Sample_Join_Linq() =
    let warmCountries = [| "Turkey"; "Italy"; "Spain"; "Saudi Arabia"; "Etiobia" |]
    let europeanCountries = [| "Denmark"; "Germany"; "Italy"; "Portugal"; "Spain" |]

    let result = query {
        for w in warmCountries do
        join e in europeanCountries on
            (w = e)
        select w
    }

    Debug.WriteLine(sprintf "Joined countries which are both warm and European:")
    for country in result do
        Debug.WriteLine(sprintf "%s" country)
Output:
Joined countries which are both warm and European:
Italy
Spain
  • C#
  • VB.Net
  • F#
static void Sample_Join_Lambda()
{
    string[] warmCountries = { "Turkey", "Italy", "Spain", "Saudi Arabia", "Etiobia" };
    string[] europeanCountries = { "Denmark", "Germany", "Italy", "Portugal", "Spain" };

    var result = warmCountries.Join(europeanCountries, warm => warm, european => european, (warm, european) => warm);

    Debug.WriteLine("Joined countries which are both warm and Europan:");
    foreach (var country in result) // Note: result is an anomymous type, thus must use a var to iterate.
        Debug.WriteLine(country);
}
Output:
Joined countries which are both warm and Europan:
Italy
Spain
Private Shared Sub Sample_Join_Lambda()
    Dim warmCountries As String() = {"Turkey", "Italy", "Spain", "Saudi Arabia", "Etiobia"}
    Dim europeanCountries As String() = {"Denmark", "Germany", "Italy", "Portugal", "Spain"}

    Dim result = warmCountries.Join(europeanCountries, Function(warm) warm, Function(european) european, Function(warm, european) warm)

    Debug.WriteLine("Joined countries which are both warm and Europan:")
    For Each country As String In result
        Debug.WriteLine(country)
    Next
End Sub
Output:
Joined countries which are both warm and Europan:
Italy
Spain
let Sample_Join_Lambda() =
    let warmCountries = [| "Turkey"; "Italy"; "Spain"; "Saudi Arabia"; "Etiobia" |]
    let europeanCountries = [| "Denmark"; "Germany"; "Italy"; "Portugal"; "Spain" |]

    let result = warmCountries.Join(europeanCountries, (fun warm -> warm), (fun european -> european), fun warm _ -> warm)

    Debug.WriteLine(sprintf "Joined countries which are both warm and European:")
    for country in result do
        Debug.WriteLine(sprintf "%s" country)
Output:
Joined countries which are both warm and European:
Italy
Spain

Share this sample on:

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