实体框架中的where子句

时间:2011-12-29 18:45:49

标签: sql-server-2005 entity-framework c#-4.0 linq-to-entities

我需要使用“where in”子句编写查询。我正在使用实体框架4。

我的SQL查询是:

select ITEMNMBR, locncode, qtyonhnd, atyalloc 
from dbo.iv00102 
where ITEMNMBR IN (
                   SELECT  cmptitnm 
                   from dbo.bm00111
                   where itemnmbr == bomItem)
AND LOCNCODE = 'MEMPHIS'

需要这样的查询:

public static Func<DBEntities, string, IQueryable<IV00102>> compiledMemphisQuery =
        CompiledQuery.Compile((DBEntities ctx, string bomNumber) =>
            from items in ctx.IV00102
            where   items.ITEMNMBR in (
                                        from orders in ctx.bm00111
                                        where orders.itemnmbr == bomItem
                                        select orders.cmpitnm)
            and items.locncode == "Memphis"
            select items);

1 个答案:

答案 0 :(得分:1)

使用查询的Contains扩展方法。它应该工作:

public static Func<DBEntities, string, IQueryable<IV00102>> compiledMemphisQuery =
    CompiledQuery.Compile((DBEntities ctx, string bomNumber) =>
        from items in ctx.IV00102
        where   (
                                    from orders in ctx.bm00111
                                    where orders.itemnmbr == bomItem
                                    select orders.cmpitnm)
        and items.locncode == "Memphis"
        select items).Contains(items.ITEMNMBR);