我正在尝试将简单的子查询转换为LINQ(c#):
select * from tblFundtoFiscalYear ftfy
where ftfy.FiscalYear = 2015
and ftfy.FundID =(
SELECT FundID
FROM tblAccountDirectParent]
where ParentAccountID = 19 )
有些事情:
var queryFTFY =
from ftfy in FRATContext.tblFundtoFiscalYear
where ftfy.FiscalYear == 2015
&& ftfy.FundID =
(from adp in FRATContext.tblAccountDirectParent
where adp.ParentAccountID == 19
select adp.FundID)
select new
{
ftfy.FundtoFiscalYearID
};
将不胜感激。
答案 0 :(得分:3)
您的SQL依赖于子查询只返回一个结果的假设。您可以在LINQ中明确指出:
var queryFTFY =
from ftfy in FRATContext.tblFundtoFiscalYear
where ftfy.FiscalYear == 2015 && ftfy.FundID ==
(
from adp in FRATContext.tblAccountDirectParent
where adp.ParentAccountID == 19
select adp.FundID
).Single()
select ftfy;
或者,您可以将查询(在两种情况下)重新表示为连接:
var queryFTFY =
from ftfy in FRATContext.tblFundtoFiscalYear
join adp in FRATContext.tblAccountDirectParent
on ftfy.FundID equals adp.FundID
where ftfy.FiscalYear == 2015
&& adp.ParentAccountID == 19
select ftfy;