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

DefaultIfEmpty (simple)

Lambda Query

DefaultIfEmpty: If a collection is empty, its default value is returned. Default value depends on collection type.

DefaultIfEmpty: If a collection is empty, its default value is returned. Default value depends on collection type.

This Lambda Expression sample returns default values for each of the empty collections, while "words" array is returned as is.

This Lambda Expression sample returns default values for each of the empty collections, while "words" array is returned as is.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for DefaultIfEmpty() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for DefaultIfEmpty() in VB.NET.
' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for DefaultIfEmpty() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
static void Sample_DefaultIfEmpty_Lambda_Simple()
{
    string[] emptyStr = { };
    int[]    emptyInt = { };
    string[] words = { "one", "two", "three" };

    var resultStr = emptyStr.DefaultIfEmpty(); // Default is null for string

    var resultInt = emptyInt.DefaultIfEmpty(); // Default is 0 for int

    var resultWords = words.DefaultIfEmpty(); // Not empty, so words array is just copied

    Debug.WriteLine("resultStr has one element with a value of null:");
    Debug.WriteLine(resultStr.Count() == 1 && resultStr.First() == null);

    Debug.WriteLine("resultInt has one element with a value of 0:");
    Debug.WriteLine(resultInt.Count() == 1 && resultInt.First() == 0);

    Debug.WriteLine("resultWords has same content as words array:");
    Debug.WriteLine(resultWords.SequenceEqual(words));
}
Output:
resultStr has one element with a value of null:
True
resultInt has one element with a value of 0:
True
resultWords has same content as words array:
True
Private Shared Sub Sample_DefaultIfEmpty_Lambda_Simple()
    Dim emptyStr As String() = {}
    Dim emptyInt As Integer() = {}
    Dim words As String() = {"one", "two", "three"}

    Dim resultStr = emptyStr.DefaultIfEmpty() ' Default is null for string

    Dim resultInt = emptyInt.DefaultIfEmpty() ' Default is 0 for int

    Dim resultWords = words.DefaultIfEmpty() ' Not empty, so words array is just copied

    Debug.WriteLine("resultStr has one element with a value of null:")
    Debug.WriteLine(resultStr.Count() = 1 AndAlso resultStr.First() Is Nothing)

    Debug.WriteLine("resultInt has one element with a value of 0:")
    Debug.WriteLine(resultInt.Count() = 1 AndAlso resultInt.First() = 0)

    Debug.WriteLine("resultWords has same content as words array:")
    Debug.WriteLine(resultWords.SequenceEqual(words))
End Sub
Output:
resultStr has one element with a value of null:
True
resultInt has one element with a value of 0:
True
resultWords has same content as words array:
True
let Sample_DefaultIfEmpty_Lambda_Simple() =
    let emptyStr:System.String list = []
    let emptyInt:System.Int32 list = []
    let words = [|"one"; "two"; "three"|]

    let resultStr = emptyStr.DefaultIfEmpty() // Default is null for string

    let resultInt = emptyInt.DefaultIfEmpty() // Default is 0 for int

    let resultWords = words.DefaultIfEmpty() // Not empty, so words array is just copied

    Debug.WriteLine(sprintf "resultStr has one element with a value of null:")
    Debug.WriteLine(sprintf "%b" (resultStr.Count() = 1 && resultStr.First() = null))

    Debug.WriteLine(sprintf "resultInt has one element with a value of 0:")
    Debug.WriteLine(sprintf "%b" (resultInt.Count() = 1 && resultInt.First() = 0))

    Debug.WriteLine("resultWords has same content as words array:")
    Debug.WriteLine(sprintf "%b" (resultWords.SequenceEqual(words)))
Output:
resultStr has one element with a value of null:
true
resultInt has one element with a value of 0:
true
resultWords has same content as words array:
true

Share this sample on:

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