简体   繁体   English

将自定义比较器与Linq IQueryable一起使用

[英]Using Custom Comparer With Linq IQueryable

I am trying to use a custom comparer on a query generated using Linq. 我正在尝试对使用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).. 我看过很多文章说您不能这样做,但是当我查看Distinct扩展方法的签名时,我看到的是:(此IQueryable源,IEqualityComparer比较器)。

Why is there a constructor that accepts a custom comparer if it cannot be converted to sql through Linq? 如果无法通过Linq将其转换为sql,为什么会有一个接受自定义比较器的构造函数?

Thanks in advance. 提前致谢。

The reason is that linq is more than just linq to sql, linq to entities or anything else. 原因是linq不仅仅是sql的linq,实体的linq或其他任何东西。

When using queryable you always have a provider. 使用可查询时,您总是有提供者。 There is no requirement for the provider to support everything that linq can do. 提供商不需要支持linq可以做的所有事情。 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 . 一般来说,我相信可以通过LINQ对IEnumerable进行的所有操作都可以对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. 我将同意,将其转换为您的提供程序链接到的任何内容都将是一件更困难的事情,但是EnumerableQueryable之间的对称性以及有人可能是我怀疑它们存在的原因这一事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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