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

Cast

Lambda Query

Cast: Casts a collection to a specified type.

Cast: Casts a collection to a specified type.

This Query Expression sample casts list of strings to a simple string array.

This Query Expression sample casts list of strings to a simple string array.

  • C#
  • VB.Net
  • F#
// Note: The source we're casting from must contain only elements of the same type.
static void Sample_Cast_Linq()
{
    List<string> vegetables = new List<string> { "Cucumber", "Tomato", "Broccoli" };

    var result = from v in vegetables.Cast<string>()
                 select v;

    Debug.WriteLine("List of vegetables casted to a simple string array:");
    foreach (string vegetable in result)
        Debug.WriteLine(vegetable);
}
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli
' Note: The source we're casting from must contain only elements of the same type.
Private Shared Sub Sample_Cast_Linq()
    Dim vegetables As New List(Of String)() From { _
        "Cucumber", _
        "Tomato", _
        "Broccoli" _
    }

    Dim result = From v In vegetables.Cast(Of String)()
                 Select v

    Debug.WriteLine("List of vegetables casted to a simple string array:")
    For Each vegetable As String In result
        Debug.WriteLine(vegetable)
    Next
End Sub
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli
// Note: The source we're casting from must contain only elements of the same type.
let Sample_Cast_Linq() =
    let vegetables = ResizeArray<string>()
    vegetables.Add("Cucumber")
    vegetables.Add("Tomato")
    vegetables.Add("Broccoli")

    let result = query {
        for v in vegetables.Cast<string>() do
        select v
    }

    Debug.WriteLine(sprintf "List of vegetables casted to a simple string array:")
    for str in vegetables do
        Debug.WriteLine(sprintf "%s" str)
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli
  • C#
  • VB.Net
  • F#
// Note: The source we're casting from must contain only elements of the same type.
static void Sample_Cast_Lambda()
{
    List<string> vegetables = new List<string> { "Cucumber", "Tomato", "Broccoli" };

    var result = vegetables.Cast<string>();

    Debug.WriteLine("List of vegetables casted to a simple string array:");
    foreach (string vegetable in result)
        Debug.WriteLine(vegetable);
}
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli
' Note: The source we're casting from must contain only elements of the same type.
Private Shared Sub Sample_Cast_Lambda()
    Dim vegetables As New List(Of String)() From { _
        "Cucumber", _
        "Tomato", _
        "Broccoli" _
    }

    Dim result = vegetables.Cast(Of String)()

    Debug.WriteLine("List of vegetables casted to a simple string array:")
    For Each vegetable As String In result
        Debug.WriteLine(vegetable)
    Next
End Sub
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli
// Note: The source we're casting from must contain only elements of the same type.
let Sample_Cast_Lambda() =
    let vegetables = ResizeArray<string>()
    vegetables.Add("Cucumber")
    vegetables.Add("Tomato")
    vegetables.Add("Broccoli")

    let result = vegetables.Cast<string>();

    Debug.WriteLine(sprintf "List of vegetables casted to a simple string array:")
    for str in vegetables do
        Debug.WriteLine(sprintf "%s" str)
Output:
List of vegetables casted to a simple string array:
Cucumber
Tomato
Broccoli

Share this sample on:

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