使用 Contains 和 bigint 进行嵌套 LINQ 查询
这是我想要的 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')
)
内部查询很简单,并在 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();
但外部查询给出错误:类型参数from ...Contains..无法从用法中推断:
var returnQuery = from o in OperOutput
where (innerQuery).Contains(o.ClearinghouseKey)
select o;
是因为 ClearinghouseKey 是 bigint 吗? 还有其他方法可以编写此查询吗?
谢谢, 珍妮
This is the SQL I want (ClearinghouseKey
is a 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')
)
The inner query is straight forward and gives correct results in 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();
But the outer query gives the error: Type arguments from ...Contains..cannot be inferred from usage:
var returnQuery = from o in OperOutput
where (innerQuery).Contains(o.ClearinghouseKey)
select o;
Is it because ClearinghouseKey is a bigint? Any other ways to write this query?
Thanks,
Jeanne
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用匿名类型:
还应该
考虑使用 Any 而不是 Contains (我还没有理由选择其中之一)。
Don't use an anonymous type:
Should be
Also consider using Any instead of Contains (I don't have reasons for choosing one over the other, yet).