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

LongCount

Lambda Query

LongCount: Returns the number of elements in collections larger than Int32.MaxValue.

LongCount: Returns the number of elements in collections larger than Int32.MaxValue.

This Lambda Expression sample counts elements in array larger than Int32.MaxValue.

This Lambda Expression sample counts elements in array larger than Int32.MaxValue.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for LongCount() in C#.
// Consider using a Lambda Expression instead.
' Use LongCount() when you expect the result to be greater than Int32.MaxValue()
Private Shared Sub Sample_LongCount_Linq()
    ' Create array which is 5 elements larger than Int32.MaxValue
    Dim largeArr = Enumerable.Range(0, Int32.MaxValue).Concat(Enumerable.Range(0, 5))

    Dim result = Aggregate a In largeArr Into LongCount()

    Debug.WriteLine("Counting largeArr elements:")
    Debug.WriteLine(result)
End Sub
Output:
Counting largeArr elements:
2147483652
// A Query Expression cannot be constructed for LongCount() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
// Use LongCount() when you expect the result to be greater than Int32.MaxValue()
static void Sample_LongCount_Lambda()
{
    // Create array which is 5 elements larger than Int32.MaxValue
    var largeArr = Enumerable.Range(0, Int32.MaxValue).Concat(Enumerable.Range(0, 5));

    Int64 result = largeArr.LongCount();

    Debug.WriteLine("Counting largeArr elements:");
    Debug.WriteLine(result);
}
Output:
Counting largeArr elements:
2147483652
' Use LongCount() when you expect the result to be greater than Int32.MaxValue()
Private Shared Sub Sample_LongCount_Lambda()
    ' Create array which is 5 elements larger than Int32.MaxValue
    Dim largeArr = Enumerable.Range(0, Int32.MaxValue).Concat(Enumerable.Range(0, 5))

    Dim result As Int64 = largeArr.LongCount()

    Debug.WriteLine("Counting largeArr elements:")
    Debug.WriteLine(result)
End Sub
Output:
Counting largeArr elements:
2147483652
// Use LongCount() when you expect the result to be greater than Int32.MaxValue()
let Sample_LongCount_Lambda() =
    // Create array which is 5 elements larger than Int32.MaxValue
    let largeArr = Enumerable.Range(0, Int32.MaxValue).Concat(Enumerable.Range(0, 5))

    let result = largeArr.LongCount();

    Debug.WriteLine(sprintf "Counting largeArr elements:")
    Debug.WriteLine(sprintf "%d" result)
Output:
Counting largeArr elements:
2147483652

Share this sample on:

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