简体   繁体   中英

Using Custom Comparer With Linq IQueryable

I am trying to use a custom comparer on a query generated using Linq. I've seen many articles saying that you cannot do that, but when I look at the signatures for the Distinct extension method I see this: (this IQueryable source, IEqualityComparer comparer)..

Why is there a constructor that accepts a custom comparer if it cannot be converted to sql through Linq?

Thanks in advance.

The reason is that linq is more than just linq to sql, linq to entities or anything else.

When using queryable you always have a provider. There is no requirement for the provider to support everything that linq can do. This is one such example.

The reason it is there is because a different provider might choose to support a different set of functionality such as the custom comparer.

In general I believe that everything that can be done to an IEnumerable through LINQ can be done to an IQueryable .

I will agree that this one is probably going to be one of the harder things to translate to whatever your provider links to but the symmetry between Enumerable and Queryable and the fact somebody might is I suspect the reason they exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM