LINQ到实体:工会鲜明+ [英] Linq to entities : Unions + Distinct

查看:110
本文介绍了LINQ到实体:工会鲜明+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我该怎么做几个工会具有鲜明。



当我使用.Distinct用的IEqualityComparer一个例外扔:




LINQ到实体无​​法识别方法System.Linq.IQueryable




我的代码是

  VAR工会= query.Union(QUERY1).Union(QUERY2); 
联合= union.Distinct(新EqualityComparerTransaction());


解决方案

LINQ到实体不支持过载鲜明这需要一个的IEqualityComparer 。当你想想,这真的不能,因为LINQ to Entities查询将被转换为SQL,你不能转换接口参考SQL。



因此​​,你必须:




  1. 使用过载鲜明不采取任何比较或

  2. 把两个列表到对象空间并执行鲜明在LINQ到对象,像这样的:

      VAR工会= query.Union(QUERY1).Union(QUERY2); 
    =工会union.AsEnumerable()是不同的(新EqualityComparerTransaction());




当然,这里的风险是,你可能带来太多的记录:从DB服务器回来。你还可以在为了做到服务器和在对象空间中的另一部分上的比较的部分中使用这两种技术


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());

解决方案

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.

这篇关于LINQ到实体:工会鲜明+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆