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

Zip

Lambda Query

Zip: Processes two collections in parallel with func instance, and combines result into a new collection.

Zip: Processes two collections in parallel with func instance, and combines result into a new collection.

This Lambda Expression sample uses Zip to process two arrays in parallel, while each processed pair is summed.

This Lambda Expression sample uses Zip to process two arrays in parallel, while each processed pair is summed.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for Zip() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for Zip() in VB.NET.
' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for Zip() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
static void Sample_Zip_Lambda()
{
    int[] numbers1 = { 1, 2, 3 };
    int[] numbers2 = { 10, 11, 12 };

    var result = numbers1.Zip(numbers2, (a, b) => (a * b));

    Debug.WriteLine("Using Zip to combine two arrays into one (1*10, 2*11, 3*12):");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Using Zip to combine two arrays into one (1*10, 2*11, 3*12):
10
22
36
Private Shared Sub Sample_Zip_Lambda()
    Dim numbers1 As Integer() = {1, 2, 3}
    Dim numbers2 As Integer() = {10, 11, 12}

    Dim result = numbers1.Zip(numbers2, Function(a, b) (a * b))

    Debug.WriteLine("Using Zip to combine two arrays into one (1*10, 2*11, 3*12):")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Using Zip to combine two arrays into one (1*10, 2*11, 3*12):
10
22
36
let Sample_Zip_Lambda() =
    let numbers1 = [|1; 2; 3|]
    let numbers2 = [|10; 11; 12|]

    let result = numbers1.Zip(numbers2, fun a b -> (a * b))

    Debug.WriteLine(sprintf "Using Zip to combine two arrays into one (1*10, 2*11, 3*12):")
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Using Zip to combine two arrays into one (1*10, 2*11, 3*12):
10
22
36

Share this sample on:

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