使用 Contains 和 bigint 进行嵌套 LINQ 查询

发布于 2024-07-12 02:57:52 字数 1078 浏览 12 评论 0原文

这是我想要的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-07-19 02:57:52

不要使用匿名类型:

select new { p.ClearinghouseKey })

还应该

select p.ClearinghouseKey)

考虑使用 Any 而不是 Contains (我还没有理由选择其中之一)。

where innerQuery.Any(i => i == o.ClearinghouseKey)

Don't use an anonymous type:

select new { p.ClearinghouseKey })

Should be

select p.ClearinghouseKey)

Also consider using Any instead of Contains (I don't have reasons for choosing one over the other, yet).

where innerQuery.Any(i => i == o.ClearinghouseKey)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文