1

在下面的 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;

    }

获取分配方法:

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;
    }
4

3 回答 3

2

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

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

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

因此,请保留您拥有的工作解决方案。

于 2013-04-09T17:38:04.747 回答
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
            };
于 2013-04-09T17:40:29.920 回答
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;  

我发布此解决方案是因为我的情况与上面不同(需要在 where 不是 select 中的子查询)。如果有人偶然发现这个线程与我有同样的问题,希望这可以为他们节省一些搜索。

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

于 2013-11-13T22:13:37.527 回答