English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ API (.Net)

We can write LINQ queries for classes that implement the IEnumerable <T> or IQueryable <T> interfaces.System.LinqThe namespace includes the following classes and interfaces required for LINQ queries.

LINQ API
 When adding a new class in Visual Studio, the default namespace System.Linq is included.

LINQ queries use extension methods for classes that implement IEnumerable or IQueryable interfaces. Enumerable and Queryable are two static classes that contain extension methods for writing LINQ queries.

Enumerable class (Enumerable)

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

The figure below shows the extension methods contained in the Enumerable class, which can be used with generic collections in C# or VB.Net.

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

Enumerable Class

Queryable

The Queryable class contains extension methods for classes that implement the member 'IQueryable<T> interface. The IQueryable<T> interface is used to provide query functionality for specific data sources of known data types, such as, the Entity Framework api implements IQueryable<T> for LINQ queries supported by the underlying database (such as MS SQL Server).

In addition, there are some APIs available for accessing third-party data. For example, LINQ to Amazon provides the functionality to combine LINQ with Amazon Web Services to search for books and other items. This can be achieved by implementing the IQueryable for Amazon interface.

The following figure shows the extension methods available in the Queryable class, which 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

  Key Points to Remember

  1. Use the System.LINQ namespace to use LINQ.

  2. The LINQ api includes two main static classes: Enumerable and Queryable.

  3. The static Enumerable class includes extension methods for classes that implement the IEnumerable<T> interface.

  4. The type of the IEnumerable<T> collection is a collection in memory, such as List, Dictionary, SortedList, Queue, HashSet, LinkedList.

  5. The static Queryable class includes extension methods for classes that implement the IQueryable<T> interface.

  6. The remote query provider implements, for example, Linq-to-SQL, LINQ-to-Amazon and others.