【问题标题】:where in clause in entity famework实体框架中的 where in 子句
【发布时间】:2011-12-29 18:45:49
【问题描述】:

我需要使用“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);

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 无论如何,这是您的查询,我认为您可以进行联接,或者在两个实体之间创建关系并使用 SelectMany。
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多