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

ToList

Lambda Query

ToList: Converts collection into a List.

ToList: Converts collection into a List.

This Lambda Expression sample converts string array to List of strings.

This Lambda Expression sample converts string array to List of strings.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for ToList() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for ToList() in VB.NET.
' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for ToList() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
static void Sample_ToList_Lambda()
{
    string[] names = { "Brenda", "Carl", "Finn" };

    List<string> result = names.ToList();

    Debug.WriteLine(String.Format("names is of type: {0}", names.GetType().Name));
    Debug.WriteLine(String.Format("result is of type: {0}", result.GetType().Name));
}
Output:
names is of type: String[]
result is of type: List`1
Private Shared Sub Sample_ToList_Lambda()
    Dim names As String() = {"Brenda", "Carl", "Finn"}

    Dim result As List(Of String) = names.ToList()

    Debug.WriteLine([String].Format("names is of type: {0}", names.[GetType]().Name))
    Debug.WriteLine([String].Format("result is of type: {0}", result.[GetType]().Name))
End Sub
Output:
names is of type: String[]
result is of type: List`1
let Sample_ToList_Lambda() =
    let names = [|"Brenda"; "Carl"; "Finn"|]
    
    let result = names.ToList()

    Debug.WriteLine(sprintf "names is of type: %s" (names.GetType().Name))
    Debug.WriteLine(sprintf "result is of type: %s" (result.GetType().Name))
Output:
names is of type: String[]
result is of type: List`1

Share this sample on:

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