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

OfType

Lambda Query

OfType: Filters elements of specified type in a collection.

OfType: Filters elements of specified type in a collection.

This Lambda Expression sample takes only objects of type string.

This Lambda Expression sample takes only objects of type string.

  • F#
  • C#
  • VB.Net
let Sample_OfType_Linq() =
    let objects : obj[] = [|"Thomas"; 31; 5.02; null; "Joey"|]

    let result = query {
        for o in objects.OfType<string>() do
        select o
    }

    Debug.WriteLine(sprintf "Objects being of type string have the values:")
    for str in result do
        Debug.WriteLine(sprintf "%s" str)
Output:
Objects being of type string have the values:
Thomas
Joey
static void Sample_OfType_Linq()
{
    object[] objects = { "Thomas", 31, 5.02, null, "Joey" };

    var result = from o in objects.OfType<string>()
                 select o;

    Debug.WriteLine("Objects being of type string:");
    foreach (string str in result)
        Debug.WriteLine(str);
}
Output:
Objects being of type string:
Thomas
Joey
Private Shared Sub Sample_OfType_Linq()
    Dim objects As Object() = {"Thomas", 31, 5.02, Nothing, "Joey"}

    Dim result = From o In objects.OfType(Of String)()

    Debug.WriteLine("Objects being of type string:")
    For Each str As String In result
        Debug.WriteLine(str)
    Next
End Sub
Output:
Objects being of type string:
Thomas
Joey
  • C#
  • VB.Net
  • F#
static void Sample_OfType_Lambda()
{
    object[] objects = { "Thomas", 31, 5.02, null, "Joey" };

    var result = objects.OfType<string>();

    Debug.WriteLine("Objects being of type string have the values:");
    foreach (string str in result)
        Debug.WriteLine(str);
}
Output:
Objects being of type string have the values:
Thomas
Joey
Private Shared Sub Sample_OfType_Lambda()
    Dim objects As Object() = {"Thomas", 31, 5.02, Nothing, "Joey"}

    Dim result = objects.OfType(Of String)()

    Debug.WriteLine("Objects being of type string have the values:")
    For Each str As String In result
        Debug.WriteLine(str)
    Next
End Sub
Output:
Objects being of type string have the values:
Thomas
Joey
let Sample_OfType_Lambda() =
    let objects : obj[] = [|"Thomas"; 31; 5.02; null; "Joey"|]

    let result = objects.OfType<string>();

    Debug.WriteLine(sprintf "Objects being of type string have the values:")
    for str in result do
        Debug.WriteLine(sprintf "%s" str)
Output:
Objects being of type string have the values:
Thomas
Joey

Share this sample on:

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