为什么我要将子查询分配给主查询之外的变量

时间:2013-04-09 17:25:28

标签: c# linq-to-entities

在下面的GetTransfers()方法中,我必须将GetAllocations()的结果分配给主查询之外的变量,否则查询将失败。为什么我要这样做?有没有更好的办法? 当查询失败时,我收到此错误:

{System.NotSupportedException:LINQ to Entities无法识别方法'System.Linq.IQueryable`1 [XCBusinessLogic.Presentation.Allocation] GetAllocations()'方法,并且此方法无法转换为商店表达式。

此查询有效:

public IQueryable<Transfer> GetTransfers()
    {
        IQueryable<Allocation> wxyz = GetAllocations();

        IQueryable<Transfer> query =
            from transfer in Context.XC_TRANSFERS
            //let wxyz = GetAllocations()
            join trader in Context.MGRS on transfer.TRADER_ID equals trader.MGR_NO
            join ssm in Context.SSM_CORES on transfer.SSM_ID equals ssm.SSM_ID
            join desk in Context.XC_DESKS on transfer.DESK_ID equals desk.DESK_ID

            select new Transfer
            {
                // snip
                _AllocationList = wxyz.Where(x => x.TRANSFER_ID == transfer.TRANSFER_ID)
            };
        return query;

    }

此查询失败:

public IQueryable<Transfer> GetTransfers()
    {
        //IQueryable<Allocation> wxyz = GetAllocations();

        IQueryable<Transfer> query =
            from transfer in Context.XC_TRANSFERS
            let wxyz = GetAllocations()
            join trader in Context.MGRS on transfer.TRADER_ID equals trader.MGR_NO
            join ssm in Context.SSM_CORES on transfer.SSM_ID equals ssm.SSM_ID
            join desk in Context.XC_DESKS on transfer.DESK_ID equals desk.DESK_ID

            select new Transfer
            {
                // snip
                _AllocationList = wxyz.Where(x => x.TRANSFER_ID == transfer.TRANSFER_ID)
            };
        return query;

    }

此查询失败:

public IQueryable<Transfer> GetTransfers()
    {
        //IQueryable<Allocation> wxyz = GetAllocations();

        IQueryable<Transfer> query =
            from transfer in Context.XC_TRANSFERS
            //let wxyz = GetAllocations()
            join trader in Context.MGRS on transfer.TRADER_ID equals trader.MGR_NO
            join ssm in Context.SSM_CORES on transfer.SSM_ID equals ssm.SSM_ID
            join desk in Context.XC_DESKS on transfer.DESK_ID equals desk.DESK_ID

            select new Transfer
            {
                // snip
                _AllocationList = GetAllocations().Where(x => x.TRANSFER_ID == transfer.TRANSFER_ID)
            };
        return query;

    }

GetAllocations方法:

public IQueryable<Allocation> GetAllocations()
    {
        IQueryable<Allocation> query =
            from alloc in Context.XC_ALLOCATIONS
            join acm in Context.ACMS on alloc.ACCT_NO equals acm.ACCT_NO
            join b in Context.BUM_DETAILS.Where(x => x.FIRM_NO == 1 && x.CATEGORY_ID == 1937) on acm.ACCT_NO equals b.ACCT_NO into bumDetails
            from bumDetail in bumDetails.DefaultIfEmpty()
            where acm.FIRM_NO == 1
            select new Allocation
            {
                AccountName = acm.ACCT_NAME
                // snip

            };
        return query;
    }

3 个答案:

答案 0 :(得分:2)

Linq to Entities将查询from transfer in Context.XC_TRANSFERS ...中的所有内容转换为SQL。因此,该查询中允许的唯一表达式是可以轻松转换为SQL的表达式。

Linq to Entities无法弄清楚GetAllocations()之类的.NET方法是如何工作的。应该怎么做?方法中可能存在任何形式的疯狂代码。怎么能把它变成SQL?

在您的情况下,该方法实际上包含另一个Linq to Entities查询。也许你可以将一个查询复制粘贴到另一个查询的内部。但我认为这不会改善您的代码!

所以,请保持您的工作解决方案。

答案 1 :(得分:1)

您可以使用join方法,然后使用into

来解决问题
IQueryable<Transfer> query =
            from transfer in Context.XC_TRANSFERS
            join allocation in GetAllocations() on transfer.TRANSFER_ID equals allocation.TRANSFER_ID into allocationList
            join trader in Context.MGRS on transfer.TRADER_ID equals trader.MGR_NO
            join ssm in Context.SSM_CORES on transfer.SSM_ID equals ssm.SSM_ID
            join desk in Context.XC_DESKS on transfer.DESK_ID equals desk.DESK_ID

            select new Transfer
            {
                // snip
                _AllocationList = allocationList
            };

答案 2 :(得分:0)

我遇到了一个非常类似的问题,Aducci的答案为我做了。这就是我想要做的事情:

query = from x in query
        where GetServicesQuery(db, options).Any(service => /*my criteria*/)
        select x;

按照Aducci的建议解决了这个问题:

query = from x in query
        join service in GetServicesQuery(db, localOptions) on x.ID equals service.ID into services
        where services.Any(service => /*my criteria*/)
        select x;  

我发布此解决方案是因为我的情况与上面不同(需要子选择的地方不是选择)。如果有人偶然发现这个问题与我有同样的问题,希望这会为他们节省一些搜索。

这一点让我很紧张,因为GetServicesQuery有很多我不想重复的标准。