LINQ API

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

LINQ is nothing but the collection of extension methods for classes that implements IEnumerable and IQueryable interface. System.Linq namespace includes the necessary classes & interfaces for LINQ. Enumerable and Queryableare two main static classes of LINQ API that contain extension methods.

System.Linq namespace is included by default when you add a new class in Visual Studio, so that you can use LINQ by default.

Enumerable:

Enumerable class includes extension methods for the classes that implement IEnumerable interface, this include all the collection types in System.Collections.Generic namespaces such as List, Dictionary, SortedList, Queue, HashSet, LinkedList etc.

The following figure illustrates that the extension methods included in Enumerable class can be used with generic collection in C# or VB.Net.

IEnumerable extension methods in Enumerable class

Queryable:

The Queryable class includes extension methods for classes that implement IQueryable interface. IQueryable is used to provide querying capabilities against a specific data source where the type of the data is known. For example, Entity Framework api implements IQueryable interface to support LINQ queries with underlaying database like SQL Server.

Also, there are APIs available to access third party data; for example, LINQ to Amazon provides the ability to use LINQ with Amazon web services to search for books and other items by implementing IQueryable interface.

The following figure illustrates that the extension methods included in Queryable class can be used with various native or third party data providers.

IQueryable extension methods in Queryable class

Visit MSDN to know all the extension methods of Enumerable and Queryable class.

Points to Remember :

  1. Use System.Linq namespace to use LINQ.
  2. LINQ api includes two main static class Enumerable & Queryable.
  3. The static Enumerable class includes extension methods for classes that implements IEnumerable interface.
  4. IEnumerable type of collections are in-memory collection like List, Dictionary, SortedList, Queue, HashSet, LinkedList
  5. The static Queryable class includes extension methods for classes that implements IQueryable interface
  6. Remote query provider implements IQueryable. eg. Linq-to-SQL, LINQ-to-Amazon etc.