Linq 到实体:联合 +清楚的
我不知道如何才能将多个联合与一个不同的联合起来。
当我将 .Distinct 与 IEqualityComparer 一起使用时,抛出异常:
LINQ to Entities 无法识别“System.Linq.IQueryable”方法
我的代码是
var union = query.Union(query1).Union(query2);
union = union.Distinct(new EqualityComparerTransaction());
I don't know how I can do several union with a distinct.
When I use .Distinct with an IEqualityComparer an exception in threw :
LINQ to Entities does not recognize the method 'System.Linq.IQueryable'
My code is
var union = query.Union(query1).Union(query2);
union = union.Distinct(new EqualityComparerTransaction());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to Entities 不支持采用
IEqualityComparer
的Distinct
重载。仔细想想,确实不能,因为 LINQ to Entities 查询将转换为 SQL,而你无法将接口引用转换为 SQL。因此,您必须:
Distinct
重载,或者将两个列表放入对象空间并在 LINQ to Objects 中执行
Distinct
,像这样:当然,这里的风险是您可能会从数据库带回太多记录服务器。您还可以使用这两种技术来在服务器上进行一部分比较,在对象空间中进行另一部分比较。
LINQ to Entities does not support the overload of
Distinct
which takes anIEqualityComparer
. When you think about it, it really can't, because LINQ to Entities queries will be converted to SQL, and you cannot convert an interface reference to SQL.Therefore, you must either:
Distinct
which does not take any compare, orBring both lists into object space and do the
Distinct
in LINQ to Objects, like this:Naturally, the risk here is that you might bring too many records back from the DB server. You could also use both of these techniques in order to do a portion of the comparison on the server and another portion in object space.
问题已经回答了,但我只是想分享我的经验。
不确定,但我认为错误消息与“我认为此参数不支持不同的方法”有关。
事实上,我们只想要 Linq to SQL ,一个可查询的表达式,它表示此属性是否相同得到其中一个。
但是当我们使用EqualityComparerTransaction这样的类时,它无法正常转换为sql。
还有另一种方法GetDistict T >(string propertyName) 但遗憾的是它并没有像我们预期的那样工作。该方法还访问数据库(我们的来源还有什么)并获取一些数据并评估不同的数据。
如果GetDistinct(string propertyName)扩展方法是做sql转换操作就可以了。但没有办法。
遗憾的是,唯一的方法是为 LINQ_TO_SQL 编写您自己的独特扩展。我认为这并不容易!因此,目前在服务器端枚举数据似乎是最简单的。
The question answered but I just want to share my experience.
Not sure but I think the error message goes with saying Distinct method not supported with this argument I think.
In fact we just want Linq to SQL , a queryable expression that says if this properties same get one of them.
But when we use a class such as EqualityComparerTransaction it can't be translated to sql normally.
There is an another method GetDistict < T >(string propertyName) But sadly it doesn't work as we expected. This method also goes to DB(what else our source) and get some data and evaluate distinct.
If GetDistinct(string propertyName) extention method were do sql convertion operation It could be. But there is no way.
Sadly the single way of doing that is coding your own distinct extention for LINQ_TO_SQL.I don't think it will be easy! So Enumarating data on the server side seems easiest for now.