LINQ到实体:工会鲜明+ [英] Linq to entities : Unions + Distinct
问题描述
我不知道我该怎么做几个工会具有鲜明。
当我使用.Distinct用的IEqualityComparer一个例外扔:
LINQ到实体无法识别方法System.Linq.IQueryable
块引用>
我的代码是
VAR工会= query.Union(QUERY1).Union(QUERY2);
联合= union.Distinct(新EqualityComparerTransaction());
解决方案LINQ到实体不支持过载
鲜明
这需要一个的IEqualityComparer
。当你想想,这真的不能,因为LINQ to Entities查询将被转换为SQL,你不能转换接口参考SQL。
因此,你必须:
- 使用过载
鲜明
不采取任何比较或
把两个列表到对象空间并执行
鲜明
在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 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:
- Use the overload of
Distinct
which does not take any compare, orBring 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屋!