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

Any

Lambda Query

Any: Checks if any elements in a collection satisifies a specified condition.

Any: Checks if any elements in a collection satisifies a specified condition.

This Lambda Expression sample checks whether any names in array start with the letter 'B'.

This Lambda Expression sample checks whether any names in array start with the letter 'B'.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for Any() in C#.
// Consider using a Lambda Expression instead.
Private Shared Sub Sample_Any_Linq()
    Dim names As String() = {"Bob", "Ned", "Amy", "Bill"}

    Dim result = Aggregate n In names Into Any()

    Debug.WriteLine("Does any of the names start with the letter 'B':")
    Debug.WriteLine(result)
End Sub
Output:
Does any of the names start with the letter 'B':
True
// There is no Any() for Query Expressions in F#, but it has the similar "exists".
let Sample_Any_Linq() =
    let names = [|"Bob"; "Ned"; "Amy"; "Bill"|]

    let result = query {
        for n in names do
        exists (n.StartsWith "B")
    }

    Debug.WriteLine(sprintf "Does any of the names start with the letter 'B':")
    Debug.WriteLine(result);
Output:
Does any of the names start with the letter 'B':
True
  • C#
  • VB.Net
  • F#
static void Sample_Any_Lambda()
{
    string[] names = { "Bob", "Ned", "Amy", "Bill" };

    var result = names.Any(n => n.StartsWith("B"));

    Debug.WriteLine("Does any of the names start with the letter 'B':");
    Debug.WriteLine(result);
}
Output:
Does any of the names start with the letter 'B':
True
Private Shared Sub Sample_Any_Lambda()
    Dim names As String() = {"Bob", "Ned", "Amy", "Bill"}

    Dim result = names.Any(Function(n) n.StartsWith("B"))

    Debug.WriteLine("Does any of the names start with the letter 'B':")
    Debug.WriteLine(result)
End Sub
Output:
Does any of the names start with the letter 'B':
True
let Sample_Any_Lambda() =
    let names = [|"Bob"; "Ned"; "Amy"; "Bill"|]

    let result = names.Any(fun n -> n.StartsWith "B")

    Debug.WriteLine(sprintf "Does any of the names start with the letter 'B':")
    Debug.WriteLine(result);
Output:
Does any of the names start with the letter 'B':
True

Share this sample on:

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