使用包含bigint的嵌套LINQ查询

时间:2009-01-11 02:26:09

标签: linq contains biginteger nested-query

这是我想要的SQL(ClearinghouseKeybigint):

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();

但是外部查询给出了错误:从...中输入参数包含..不能从用法中推断:

var returnQuery = from o in OperOutput
                  where (innerQuery).Contains(o.ClearinghouseKey)
                  select o;

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

谢谢, 珍

1 个答案:

答案 0 :(得分:5)

不要使用匿名类型:

select new { p.ClearinghouseKey })

应该是

select p.ClearinghouseKey)

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

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