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

Intersect

Lambda Query

Intersect: Takes only the elements that are shared between two collections.

Intersect: Takes only the elements that are shared between two collections.

This Lambda Expression sample creates a new collection with values shared between the two arrays.

This Lambda Expression sample creates a new collection with values shared between the two arrays.

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

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

    Debug.WriteLine("Intersect creates a single sequence with only the duplicates:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Intersect creates a single sequence with only the duplicates:
3
Private Shared Sub Sample_Intersect_Linq()
    Dim numbers1 As Integer() = {1, 2, 3}
    Dim numbers2 As Integer() = {3, 4, 5}

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

    Debug.WriteLine("Intersect creates a single sequence with only the duplicates:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Intersect creates a single sequence with only the duplicates:
3
let Sample_Intersect_Linq() =
    let numbers1 = [| 1; 2; 3 |]
    let numbers2 = [| 3; 4; 5 |]

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

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

    var result = numbers1.Intersect(numbers2);

    Debug.WriteLine("Intersect creates a single sequence with only the duplicates:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Intersect creates a single sequence with only the duplicates:
3
Private Shared Sub Sample_Intersect_Lambda()
    Dim numbers1 As Integer() = {1, 2, 3}
    Dim numbers2 As Integer() = {3, 4, 5}

    Dim result = numbers1.Intersect(numbers2)

    Debug.WriteLine("Intersect creates a single sequence with only the duplicates:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Intersect creates a single sequence with only the duplicates:
3
let Sample_Intersect_Lambda() =
    let numbers1 = [| 1; 2; 3 |]
    let numbers2 = [| 3; 4; 5 |]

    let result = numbers1.Intersect(numbers2)

    Debug.WriteLine(sprintf "Intersect creates a single sequence with only the duplicates:")
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Intersect creates a single sequence with only the duplicates:
3

Share this sample on:

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