从 linq 查询调用函数时出错
这是我编写的代码,当我添加调用第二个函数的行时,我就收到错误 ValidCodes = GetValidCodes(bv.Variable_Id)
public IQueryable<BvIndexRow> GetBenefitVariableIndex(int healthPlanId)
{
var q = from bv in Db.BenefitVariables
where bv.Private == "N" || (bv.Private == "Y" && bv.Health_Plan_ID == healthPlanId)
join bao in Db.baObject on bv.Variable_Id equals bao.ba_Object_id
join servicetype in Db.BenefitVariableServiceTypes.Where(bvst => bvst.Key_Service_Type == "Y" && bvst.isActive == 1)
on bv.Variable_Id equals servicetype.Variable_Id into groupedBvst
where bv.isActive == 1 && bao.isActive == 1
from bvst in groupedBvst.DefaultIfEmpty()
select new BvIndexRow
{
// some code here too
ValidCodes = GetValidCodes(bv.Variable_Id)
};
return q;
}
public string GetValidCodes(int varID)
{
// some code here
return "None";
}
Here the code which i have written, I am getting error as soon as i added the line to call the second function which is ValidCodes = GetValidCodes(bv.Variable_Id)
public IQueryable<BvIndexRow> GetBenefitVariableIndex(int healthPlanId)
{
var q = from bv in Db.BenefitVariables
where bv.Private == "N" || (bv.Private == "Y" && bv.Health_Plan_ID == healthPlanId)
join bao in Db.baObject on bv.Variable_Id equals bao.ba_Object_id
join servicetype in Db.BenefitVariableServiceTypes.Where(bvst => bvst.Key_Service_Type == "Y" && bvst.isActive == 1)
on bv.Variable_Id equals servicetype.Variable_Id into groupedBvst
where bv.isActive == 1 && bao.isActive == 1
from bvst in groupedBvst.DefaultIfEmpty()
select new BvIndexRow
{
// some code here too
ValidCodes = GetValidCodes(bv.Variable_Id)
};
return q;
}
public string GetValidCodes(int varID)
{
// some code here
return "None";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一位发帖者回答了原因,但没有回答该怎么做。原因是方法调用无法通过 Linq-to-SQL 转换为 SQL,因为它...不是 SQL 的一部分!这很有道理!所以解决这个问题的一个简单方法是:
这应该给你想要的!
编辑:顺便说一句,您可能还想在 BvIndexRows 列表上调用 .ToList() 或 .ToArray() 。这具有引起立即评估的效果并且将防止多重枚举。如果您不想立即评估,您可能需要编辑您的问题以解释为什么会出现这种情况。我在上面的示例中添加了 .ToArray(),但我也想解释一下原因!
Another poster has answered why, but not what to do. The why is that the method call cannot be translated by Linq-to-SQL into SQL, as it's... not part of SQL! This makes perfect sense! So an easy way to fix this is:
This should give you want you want!
EDIT: By the by, you probably also want to call .ToList() or .ToArray() on your list of BvIndexRows. This has the effect of causing immediate evaluation and will prevent a multiple enumeration. If you don't want immediate evaluation you may need to edit your question to explain why that's the case. I've added a .ToArray() to the example above, but I also wanted to explain why!
Linq to Sql 无法将方法调用
GetValidCodes(..)
解析为数据库上下文中的操作 - 将结果带回内存后,您必须填写该属性,或者填写直接在相应的linq to sql语句中。Linq to Sql cannot resolve the method call
GetValidCodes(..)
into an operation in the context of the database - you will have to fill in that property once you have brought back your results into memory or alternatively fill in the corresponding linq to sql statements directly.