LINQ API

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 Queryable are 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<T> interface, this include all the collection types in System.Collections.Generic namespaces such as List<T>, Dictionary<T>, SortedList<T>, Queue<T>, HashSet<T>, LinkedList<T> 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<T> extension methods in Enumerable class

Queryable:

The Queryable class includes extension methods for classes that implement IQueryable<t> interface. IQueryable<T> 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<T> 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<T> interface.
  4. IEnumerable<T> 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<T> interface
  6. Remote query provider implements IQueryable<T>. eg. Linq-to-SQL, LINQ-to-Amazon etc.