C# 为什么要将do子查询分配给主查询之外的变量
在下面的GetTransfers()方法中,我必须将GetAllocations()的结果分配给主查询之外的变量,否则查询将失败。我为什么要这么做?有更好的办法吗? 当查询失败时,我得到以下错误: {System.NotSupportedException:LINQ to Entities无法识别方法'System.LINQ.IQueryable`1[XCBusinessLogic.Presentation.Allocation]GetAllocations()'方法,并且无法将此方法转换为存储表达式 此查询可用于:C# 为什么要将do子查询分配给主查询之外的变量,c#,linq-to-entities,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;
}
publicIQueryable GetTransfers()
{
IQueryable wxyz=GetAllocations();
可查询查询=
从Context.XC\u传输中的传输
//设wxyz=GetAllocations()
在传输时加入Context.MGRS中的trader.trader\u ID等于trader.MGR\u NO
在Context.ssm_CORES on transfer.ssm_ID等于ssm.ssm_ID中加入ssm
在Context.XC_DESKS on transfer.desk_ID等于desk.desk_ID
选择新传输
{
//剪断
_AllocationList=wxyz.Where(x=>x.TRANSFER\u ID==TRANSFER.TRANSFER\u ID)
};
返回查询;
}
此查询失败:
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;
}
publicIQueryable GetTransfers()
{
//IQueryable wxyz=GetAllocations();
可查询查询=
从Context.XC\u传输中的传输
设wxyz=GetAllocations()
在传输时加入Context.MGRS中的trader.trader\u ID等于trader.MGR\u NO
在Context.ssm_CORES on transfer.ssm_ID等于ssm.ssm_ID中加入ssm
在Context.XC_DESKS on transfer.desk_ID等于desk.desk_ID
选择新传输
{
//剪断
_AllocationList=wxyz.Where(x=>x.TRANSFER\u ID==TRANSFER.TRANSFER\u ID)
};
返回查询;
}
此查询失败:
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;
}
publicIQueryable GetTransfers()
{
//IQueryable wxyz=GetAllocations();
可查询查询=
从Context.XC\u传输中的传输
//设wxyz=GetAllocations()
在传输时加入Context.MGRS中的trader.trader\u ID等于trader.MGR\u NO
在Context.ssm_CORES on transfer.ssm_ID等于ssm.ssm_ID中加入ssm
在Context.XC_DESKS on transfer.desk_ID等于desk.desk_ID
选择新传输
{
//剪断
_AllocationList=GetAllocations()。其中(x=>x.TRANSFER\u ID==TRANSFER.TRANSFER\u ID)
};
返回查询;
}
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;
}
publicIQueryable GetAllocations()
{
可查询查询=
从Context.XC_分配中的alloc
将acm加入Context.ACMS on alloc.ACCT\u NO等于acm.ACCT\u NO
在Context.BUM\u DETAILS中加入b,其中(x=>x.FIRM\u NO==1&&x.CATEGORY\u ID==1937)在acm.ACCT\u NO上等于b.ACCT\u NO进入BUM DETAILS
从bumDetails.DefaultIfEmpty()中的bumDetail
其中acm.FIRM_NO==1
选择新分配
{
AccountName=acm.ACCT\u NAME
//剪断
};
返回查询;
}
Linq to Entities将查询中的所有内容从上下文中的传输转换为SQL。XC\u将…转换为SQL。因此,该查询中允许的唯一表达式是可以轻松转换为SQL的表达式
Linq to Entities无法理解像GetAllocations()
这样的.NET方法是如何工作的。它应该如何工作?方法中可能存在任何形式的疯狂代码。它如何将其转换为SQL
在您的例子中,该方法实际上包含另一个Linq to Entities查询。也许您可以将一个查询复制粘贴到另一个查询的内部。但我认为这不会改进您的代码
因此,只需保留您现有的有效解决方案。您可以通过使用
join
和您的方法,然后在中加入
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
};
IQueryable查询=
从Context.XC\u传输中的传输
将transfer.transfer\u ID等于allocation.transfer\u ID的GetAllocations()中的分配加入allocationList
在传输时加入Context.MGRS中的trader.trader\u ID等于trader.MGR\u NO
在Context.ssm_CORES on transfer.ssm_ID等于ssm.ssm_ID中加入ssm
在Context.XC_DESKS on transfer.desk_ID等于desk.desk_ID
选择新传输
{
//剪断
_AllocationList=AllocationList
};
我遇到了一个非常类似的问题,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有很多标准,我不想重复。我现在爱你!请看下面我遇到的问题以及这有什么帮助!