LINQ到实体 - 多列where..in条款 [英] LINQ to Entities - where..in clause with multiple columns
问题描述
我试图使用LINQ到-EF查询表格的数据:
类位置{
串国家;
线城市;
字符串地址;
:
}
由元组查找的位置(国家,城市,地址)。我试过
VAR键=新[] {
新{国家= ...,市= ...,地址= ... }
:
}
VAR的结果=从位置
禄在那里keys.Contains(新{
=国家loc.Country,
市= loc.City,
地址= loc.Address
}
但LINQ并不想接受匿名类型(我的理解是表达的LINQ元组的方式),以包含参数()。
有一个好的方式来表达这在LINQ,同时能够对数据库运行查询或者,如果我只是遍历键和联盟() - 编查询在一起,那会是坏的性能。
如何:
VAR结果= locations.Where(L => keys.Any(K =>
k.Country == l.Country&放大器;&安培;
k.City == l.City&放大器;&安培;
k.Address == l.Address));
更新
不幸的是EF抛出上引发NotSupportedException,如果你需要查询,以便对数据库端运行的不够格这个答案。
更新2
试过各种使用自定义类和元组加入的 - 既没有工作。哪些数据量,我们在谈论什么?如果没什么太大了,你既可以处理它的客户端(方便)或使用工会(如果不是更快,至少传递的数据更少)。
I'm trying to query data of the form with LINQ-to-EF:
class Location {
string Country;
string City;
string Address;
…
}
by looking up a location by the tuple (Country, City, Address). I tried
var keys = new[] {
new {Country=…, City=…, Address=…},
…
}
var result = from loc in Location
where keys.Contains(new {
Country=loc.Country,
City=loc.City,
Address=loc.Address
}
but LINQ doesn't want to accept an anonymous type (which I understand is the way to express tuples in LINQ) as the parameter to Contains().
Is there a "nice" way to express this in LINQ, while being able to run the query on the database? Alternately, if I just iterated over keys and Union()-ed the queries together, would that be bad for performance?
How about:
var result = locations.Where(l => keys.Any(k =>
k.Country == l.Country &&
k.City == l.City &&
k.Address == l.Address));
UPDATE
Unfortunately EF throws NotSupportedException on that, which disqualifies this answer if you need the query to run on DB side.
UPDATE 2
Tried all kinds of joins using custom classes and Tuples - neither works. What data volumes are we talking about? If it's nothing too big, you could either process it client-side (convenient) or use unions (if not faster, at least less data is transmitted).
这篇关于LINQ到实体 - 多列where..in条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!