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

GroupJoin (left outer join)

Lambda Query

GroupJoin: Groups two collections by a common key value, and is imilar to left outer join in SQL.

GroupJoin: Groups two collections by a common key value, and is imilar to left outer join in SQL.

This Query Expression sample groups collection "persons" with collection "languages" by a common key.

This Query Expression sample groups collection "persons" with collection "languages" by a common key.

  • C#
  • VB.Net
// There is no GroupJoin() for Query Expressions in C#, but
// it has the similar "join...in...on...equals...into".
class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Person
{
    public int LanguageId { get; set; }
    public string FirstName { get; set; }
}

static void Sample_GroupJoin_Linq()
{
    Language[] languages = new Language[]
    {
        new Language {Id = 1, Name = "English"},
        new Language {Id = 2, Name = "Russian"}
    };

    Person[] persons = new Person[]
    {
        new Person { LanguageId = 1, FirstName = "Tom" },
        new Person { LanguageId = 1, FirstName = "Sandy" },
        new Person { LanguageId = 2, FirstName = "Vladimir" },
        new Person { LanguageId = 2, FirstName = "Mikhail" },
    };

    var result = from lang in languages
                 join pers in persons
                 on lang.Id
                 equals pers.LanguageId into ps
                 select new
                 {
                     Key = lang.Name,
                     Persons = ps
                 };

    Debug.WriteLine("Group joined list of people speaking either English or Russian:");
    foreach (var language in result)
    {
        Debug.WriteLine(String.Format("People speaking {0}:", language.Key));

        foreach (var person in language.Persons)
        {
            Debug.WriteLine(person.FirstName);
        }
    }
}
Output:
Group joined list of people speaking either English or Russian:
People speaking English:
Tom
Sandy
People speaking Russian:
Vladimir
Mikhail
' There is no GroupJoin() for Query Expressions in VB.NET, but
' it has the similar "Group Join...In...On...Equals...Into".
class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Person
{
    public int LanguageId { get; set; }
    public string FirstName { get; set; }
}

Private Shared Sub Sample_GroupJoin_Linq()
    Dim languages As Language() = New Language() {New Language() With { _
        .Id = 1, .Name = "English" _
    }, New Language() With { _
        .Id = 2, .Name = "Russian" _
    }}

    Dim persons As Person() = New Person() {New Person() With { _
        .LanguageId = 1, .FirstName = "Tom" _
    }, New Person() With { _
        .LanguageId = 1, .FirstName = "Sandy" _
    }, New Person() With { _
        .LanguageId = 2, .FirstName = "Vladimir" _
    }, New Person() With { _
        .LanguageId = 2, .FirstName = "Mikhail" _
    }}

    Dim result = From lang In languages
                 Group Join pers In persons
                 On lang.Id Equals pers.LanguageId Into ps = Group
                 Select New With { _
                    .Key = lang.Name, _
                    .Persons = ps _
                 }

    Debug.WriteLine("Group-joined list of people speaking either English or Russian:")
    For Each language In result
        Debug.WriteLine([String].Format("Persons speaking {0}:", language.Key))

        For Each person In language.Persons
            Debug.WriteLine(person.FirstName)
        Next
    Next
End Sub
Output:
Group-joined list of people speaking either English or Russian:
Persons speaking English:
Tom
Sandy
Persons speaking Russian:
Vladimir
Mikhail
  • C#
  • VB.Net
class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Person
{
    public int LanguageId { get; set; }
    public string FirstName { get; set; }
}

static void Sample_GroupJoin_Lambda()
{
    Language[] languages = new Language[]
    {
        new Language {Id = 1, Name = "English"},
        new Language {Id = 2, Name = "Russian"}
    };

    Person[] persons = new Person[]
    {
        new Person { LanguageId = 1, FirstName = "Tom" },
        new Person { LanguageId = 1, FirstName = "Sandy" },
        new Person { LanguageId = 2, FirstName = "Vladimir" },
        new Person { LanguageId = 2, FirstName = "Mikhail" },
    };

    var result = languages.GroupJoin(persons, lang => lang.Id, pers => pers.LanguageId, 
        (lang, ps) => new { Key = lang.Name, Persons = ps });

    Debug.WriteLine("Group-joined list of people speaking either English or Russian:");
    foreach (var language in result)
    {
        Debug.WriteLine(String.Format("Persons speaking {0}:", language.Key));

        foreach (var person in language.Persons)
        {
            Debug.WriteLine(person.FirstName);
        }
    }
}
Output:
Group-joined list of people speaking either English or Russian:
Persons speaking English:
Tom
Sandy
Persons speaking Russian:
Vladimir
Mikhail
Private Class Language
    Public Property Id() As Integer
    Public Property Name() As String
End Class

Private Class Person
    Public Property LanguageId() As Integer
    Public Property FirstName() As String
End Class

Private Shared Sub Sample_GroupJoin_Lambda()
    Dim languages As Language() = New Language() {New Language() With { _
        .Id = 1, .Name = "English" _
    }, New Language() With { _
        .Id = 2, .Name = "Russian" _
    }}

    Dim persons As Person() = New Person() {New Person() With { _
        .LanguageId = 1, .FirstName = "Tom" _
    }, New Person() With { _
        .LanguageId = 1, .FirstName = "Sandy" _
    }, New Person() With { _
        .LanguageId = 2, .FirstName = "Vladimir" _
    }, New Person() With { _
        .LanguageId = 2, .FirstName = "Mikhail" _
    }}

    Dim result = languages.GroupJoin(persons, Function(lang) lang.Id, Function(pers) pers.LanguageId, _
                 Function(lang, ps) New With {.Key = lang.Name, .Persons = ps})

    Debug.WriteLine("Group-joined list of people speaking either English or Russian:")
    For Each language In result
        Debug.WriteLine([String].Format("Persons speaking {0}:", language.Key))

        For Each person In language.Persons
            Debug.WriteLine(person.FirstName)
        Next
    Next
End Sub
Output:
Group-joined list of people speaking either English or Russian:
Persons speaking English:
Tom
Sandy
Persons speaking Russian:
Vladimir
Mikhail

Share this sample on:

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