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

Except

Lambda Query

Except: Removes all elements from one collection which exist in another collection.

Except: Removes all elements from one collection which exist in another collection.

This Lambda Expression sample removes numbers from "numbers1", which exist in "numbers2".

This Lambda Expression sample removes numbers from "numbers1", which exist in "numbers2".

  • C#
  • VB.Net
  • F#
static void Sample_Except_Linq()
{
    int[] numbers1 = { 1, 2, 3 };
    int[] numbers2 = { 3, 4, 5 };

    var result = (from n in numbers1.Except(numbers2)
                  select n);

    Debug.WriteLine("Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2
Private Shared Sub Sample_Except_Linq()
    Dim numbers1 As Integer() = {1, 2, 3}
    Dim numbers2 As Integer() = {3, 4, 5}

    Dim result = From n In numbers1.Except(numbers2)
                 Select n

    Debug.WriteLine("Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2
let Sample_Except_Linq() =
    let numbers1 = [| 1; 2; 3 |]
    let numbers2 = [| 3; 4; 5 |]

    let result = query {
        for n in numbers1.Except(numbers2) do
        select n
    }

    Debug.WriteLine(sprintf "Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:")
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2
  • C#
  • VB.Net
  • F#
static void Sample_Except_Lambda()
{
    int[] numbers1 = { 1, 2, 3 };
    int[] numbers2 = { 3, 4, 5 };

    var result = numbers1.Except(numbers2);

    Debug.WriteLine("Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2
Private Shared Sub Sample_Except_Lambda()
    Dim numbers1 As Integer() = {1, 2, 3}
    Dim numbers2 As Integer() = {3, 4, 5}

    Dim result = numbers1.Except(numbers2)

    Debug.WriteLine("Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2
let Sample_Except_Lambda() =
    let numbers1 = [| 1; 2; 3 |]
    let numbers2 = [| 3; 4; 5 |]

    let result = numbers1.Except(numbers2)

    Debug.WriteLine(sprintf "Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:")
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Except creates a single sequence from numbers1 and removes the duplicates found in numbers2:
1
2

Share this sample on:

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