Conversions in LINQ


Advertisements

The operators change the type of input objects and are used in a diverse range of applications.

Operator Description C# Query Expression Syntax VB Query Expression Syntax
AsEnumerable Returns the input typed as IEnumerable<T> Not Applicable Not Applicable
AsQueryable A (generic) IEnumerable is converted to a (generic) IQueryable Not Applicable Not Applicable
Cast Performs casting of elements of a collection to a specified type Use an explicitly typed range variable. Eg:from string str in words From … As …
OfType Filters values on basis of their , depending on their capability to be cast to a particular type Not Applicable Not Applicable
ToArray Forces query execution and does conversion of a collection to an array Not Applicable Not Applicable
ToDictionary On basis of a key selector function set elements into a Dictionary<TKey, TValue> and forces execution of a LINQ query Not Applicable Not Applicable
ToList Forces execution of a query by converting a collection to a List<T> Not Applicable Not Applicable
ToLookup Forces execution of a query and put elements into a Lookup<TKey, TElement> on basis of a key selector function Not Applicable Not Applicable

Example of Cast - Query Expression

C#

using System;
using System.Linq;

namespace Operators {
   class Cast {
      static void Main(string[] args) {
      
         Plant[] plants = new Plant[] {new CarnivorousPlant { Name = "Venus Fly Trap", TrapType = "Snap Trap" },
                          new CarnivorousPlant { Name = "Pitcher Plant", TrapType = "Pitfall Trap" },
                          new CarnivorousPlant { Name = "Sundew", TrapType = "Flypaper Trap" },
                          new CarnivorousPlant { Name = "Waterwheel Plant", TrapType = "Snap Trap" }};

         var query = from CarnivorousPlant cPlant in plants
                     where cPlant.TrapType == "Snap Trap"
                     select cPlant;

         foreach (var e in query) {
            Console.WriteLine("Name = {0} , Trap Type = {1}", e.Name, e.TrapType);
         }

         Console.WriteLine("\nPress any key to continue.");
         Console.ReadKey();
      }
   }

   class Plant {
      public string Name { get; set; }
   }

   class CarnivorousPlant : Plant {
      public string TrapType { get; set; }
   }
}

VB

Module Module1
   Sub Main()

      Dim plants() As Plant = {New CarnivorousPlant With {.Name = "Venus Fly Trap", .TrapType = "Snap Trap"},
                              New CarnivorousPlant With {.Name = "Pitcher Plant", .TrapType = "Pitfall Trap"},
                              New CarnivorousPlant With {.Name = "Sundew", .TrapType = "Flypaper Trap"},
                              New CarnivorousPlant With {.Name = "Waterwheel Plant", .TrapType = "Snap Trap"}}

      Dim list = From cPlant As CarnivorousPlant In plants
                 Where cPlant.TrapType = "Snap Trap"
                 Select cPlant

      For Each e In list
         Console.WriteLine("Name = {0} , Trap Type = {1}", e.Name, e.TrapType)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()
   End Sub

   Class Plant
      Public Property Name As String
   End Class

   Class CarnivorousPlant
      Inherits Plant
      Public Property TrapType As String
   End Class

End Module

When the above code in C# or VB is compiled and executed, it produces the following result −

Name = Venus Fly Trap, TrapType = Snap Trap
Name = Waterwheel Plant, TrapType = Snap Trap

Press any key to continue.
linq_query_operators.htm
Advertisements