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 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.
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.
Points to Remember :
- Use System.Linq namespace to use LINQ.
- LINQ api includes two main static class Enumerable & Queryable.
- The static Enumerable class includes extension methods for classes that implements IEnumerable<T> interface.
- IEnumerable<T> type of collections are in-memory collection like List, Dictionary, SortedList, Queue, HashSet, LinkedList
- The static Queryable class includes extension methods for classes that implements IQueryable<T> interface
- Remote query provider implements IQueryable<T>. eg. Linq-to-SQL, LINQ-to-Amazon etc.