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

Range

Lambda Query

Range: Generates sequence of numeric values.

Range: Generates sequence of numeric values.

This Lambda Expression sample generates sequence within a range from 0-10.

This Lambda Expression sample generates sequence within a range from 0-10.

  • C#
  • VB.Net
  • F#
static void Sample_Range_Linq()
{
    var result = from n in Enumerable.Range(0, 10)
                 select n;

    Debug.WriteLine("Counting from 0 to 9:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9
Private Shared Sub Sample_Range_Linq()
    Dim result = From n In Enumerable.Range(0, 10)

    Debug.WriteLine("Counting from 0 to 9:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9
let Sample_Range_Linq() =
    let result = query {
        for n in Enumerable.Range(0, 10) do
        select n
    }

    Debug.WriteLine(sprintf "Counting from 0 to 9:");
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9
  • C#
  • VB.Net
  • F#
static void Sample_Range_Lambda()
{
    var result = Enumerable.Range(0, 10);

    Debug.WriteLine("Counting from 0 to 9:");
    foreach (int number in result)
        Debug.WriteLine(number);
}
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9
Private Shared Sub Sample_Range_Lambda()
    Dim result = Enumerable.Range(0, 10)

    Debug.WriteLine("Counting from 0 to 9:")
    For Each number As Integer In result
        Debug.WriteLine(number)
    Next
End Sub
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9
let Sample_Range_Lambda() =
    let result = Enumerable.Range(0, 10)

    Debug.WriteLine(sprintf "Counting from 0 to 9:")
    for number in result do
        Debug.WriteLine(sprintf "%d" number)
Output:
Counting from 0 to 9:
0
1
2
3
4
5
6
7
8
9

Share this sample on:

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