LINQ Providers C# Help

.NET 3.5 includes several LINQ providers. A LINQ provider implements the standard query operators for a specific data source. LINQ providers might implement more extension methods that are defined by LINQ, but the standard operators at least must be implemented. LINQ to XMLimplements more methods that are particularly useful with XML, for example the methods Elements (),Descendants, and Ancestors are defined by the class Extensions in the System.Xml .Ling namespace. The implementation of the LINQ provider is selected based on the namespace and on the type of the first parameter. The namespace of the class that implements the extension methods must be opened, otherwise the extension class is not in scope. The parameter of the Where () method that is defined by LINQ to objects and the Where () method that is defined by LINQ to SQL is different.

The Where () method of LINQ to objects is defined with the Enumerable class:

public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Inside the System. Linq namespace there’s another “lass that implements the operator Where. This implementation is used by LINQ to SQL. You can find the implementation in the class Queryable:

public static lQueryable<TSource> Where<TSource>(
this IQueryable<TSource> source, Expression<Func<TSource, bool» predicate);

Both of these classes are implemented in the System. Core assembly in the System. Linq namespace. How is it defined and what method is used? The Lambda expression is the same no matter whether it is passed with a Func<TSource, boo L> parameter or with an Expression<F1lnc<TSource, boo l> ‘parameter. Just ~ compiler behaves differently. The selection is done based on the source parameter. The method that matches best based on its parameters is chosen by the compiler. The Gettable ( ) method of the DataContext class that is defined by LINQ to SQLreturns lQueryab1e<TSource>, and , thus LINQ to SQLuses the Where () method of the Queryable class.

The LINQ to SQL provider is a provider that makes use of expression trees and implements the interfaces lQueryable and IQueryProvider.

Posted on October 30, 2015 in Language Integrated Query

Share the Story

Back to Top