Linq 到实体:联合 +清楚的

发布于 2024-08-22 00:43:13 字数 316 浏览 6 评论 0原文

我不知道如何才能将多个联合与一个不同的联合起来。

当我将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江挽川 2024-08-29 00:43:13

LINQ to Entities 不支持采用 IEqualityComparerDistinct 重载。仔细想想,确实不能,因为 LINQ to Entities 查询将转换为 SQL,而你无法将接口引用转换为 SQL。

因此,您必须:

  1. 使用不进行任何比较的 Distinct 重载,或者
  2. 将两个列表放入对象空间并在 LINQ to Objects 中执行 Distinct ,像这样:

    var union = query.Union(query1).Union(query2);
    union = union.AsEnumerable().Distinct(new EqualityComparerTransaction());
    

当然,这里的风险是您可能会从数据库带回太多记录服务器。您还可以使用这两种技术来在服务器上进行一部分比较,在对象空间中进行另一部分比较。

LINQ to Entities does not support the overload of Distinct which takes an IEqualityComparer. 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:

  1. Use the overload of Distinct which does not take any compare, or
  2. Bring both lists into object space and do the Distinct in LINQ to Objects, like this:

    var union = query.Union(query1).Union(query2);
    union = union.AsEnumerable().Distinct(new EqualityComparerTransaction());
    

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.

把梦留给海 2024-08-29 00:43:13

问题已经回答了,但我只是想分享我的经验。

不确定,但我认为错误消息与“我认为此参数不支持不同的方法”有关。

事实上,我们只想要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文