LINQ API

We can write LINQ queries for the classes that implement IEnumerable<T> or IQueryable<T> interface. The System.Linq namespace includes the following classes and interfaces require for LINQ queries.

LINQ API
tip System.Linq namespace is included by default when you add a new class in Visual Studio.

LINQ queries uses extension methods for classes that implement IEnumerable or IQueryable interface. The Enumerable and Queryable are two static classes that contain extension methods to write LINQ queries.

Enumerable

The Enumerable class includes extension methods for the classes that implement IEnumerable<T> interface, for example all the built-in collection classes implement IEnumerable<T> interface and so we can write LINQ queries to retrieve data from the built-in collections.

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

The following figure shows all the extension methods available in Enumerable class.

Enumerable Class

Queryable

The Queryable class includes extension methods for classes that implement IQueryable<t> interface. The IQueryable<T> interface 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 databases such as MS 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. This can be achieved by implementing the IQueryable interface for Amazon.

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

The following figure shows the extension methods available in the Queryable class.

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 the 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 the IQueryable<T> interface.
  6. Remote query provider implements e.g. Linq-to-SQL, LINQ-to-Amazon etc.