使用包含bigint的嵌套LINQ查询

时间:2021-11-20 16:49:39

This is the SQL I want (ClearinghouseKey is a bigint):

这是我想要的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')
)

The inner query is straight forward and gives correct results in LINQPad:

内部查询是直接的,并在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?

是因为ClearinghouseKey是一个bigint?还有其他任何方式来编写此查询吗?

Thanks, Jeanne

1 个解决方案

#1


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).

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

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

#1


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).

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

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