带有子查询的IN子句的实体框架语法

时间:2015-10-06 14:01:54

标签: asp.net-mvc database entity-framework

我想要一个带有子查询的IN子句的Entity Framework sql语法。 我原来的语法就像

Select * from Table1 where Table1.id in (select table2.id from Table2 where 1=1)

我试着写下面这个:

"SELECT VALUE pkg_outer from 
   (SELECT distinct(pkg) from ExploitaEntities.mst_package as pkg  WHERE 1=1 
   AND pkg.package_id in 
   (SELECT VALUE dtl_outer FROM(select distinct(dtl.package_id) from ExploitaEntities.mst_pkg_detail as dtl where 1=1)) 
   as dtl_outer) 
as pkg_outer"

但它给我的错误如下:

"The element type 'Edm.Int32' and the CollectionType 'Transient.collection[Transient.rowtype[(package_id,Edm.Int32(Nullable=True,DefaultValue=))](Nullable=True,DefaultValue=)]' 
 are not compatible. The IN expression only supports entity, primitive, and reference types.  
 Near IN set expression,..."

1 个答案:

答案 0 :(得分:1)

dbContext.TableName.Where(x => x.ColumnName.Value == "Test")

在Db上下文中引用表。您可以为每个读取的记录设置条件。