1 linq contains biginteger nested-query
这是我想要的SQL(ClearinghouseKey是bigint):
select *
from ConsOutput O
where O.ClearinghouseKey IN (
select distinct P.clearinghouseKey
from Project P
Inner join LandUseInProject L on L.ClearinghouseKey = P.ClearinghouseKey
where P.ProjectLocationKey IN ('L101', 'L102', 'L103')
and L.LandUseKey IN ('U000', 'U001', 'U002', 'U003')
)
Run Code Online (Sandbox Code Playgroud)
内部查询是直接的,并在LINQPad中给出正确的结果:
var innerQuery = (from p in Projects
join l in LandUseInProjects on p.ClearinghouseKey equals l.ClearinghouseKey
where locations.Contains(p.ProjectLocationKey)
&& (landuses.Contains(l.LandUseKey))
select new { p.ClearinghouseKey }).Distinct();
Run Code Online (Sandbox Code Playgroud)
但是外部查询给出了错误:从...中键入参数包含..无法从用法推断:
var returnQuery = from o in OperOutput
where (innerQuery).Contains(o.ClearinghouseKey)
select o;
Run Code Online (Sandbox Code Playgroud)
是因为ClearinghouseKey是一个bigint?还有其他任何方式来编写此查询吗?
谢谢,珍妮
不要使用匿名类型:
select new { p.ClearinghouseKey })
Run Code Online (Sandbox Code Playgroud)
应该
select p.ClearinghouseKey)
Run Code Online (Sandbox Code Playgroud)
另外考虑使用Any而不是Contains(我没有理由选择其中一个而不是).
where innerQuery.Any(i => i == o.ClearinghouseKey)
Run Code Online (Sandbox Code Playgroud)