从 linq 查询调用函数时出错

发布于 2024-12-08 01:24:04 字数 1023 浏览 1 评论 0原文

这是我编写的代码,当我添加调用第二个函数的行时,我就收到错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-12-15 01:24:04

另一位发帖者回答了原因,但没有回答该怎么做。原因是方法调用无法通过 Linq-to-SQL 转换为 SQL,因为它...不是 SQL 的一部分!这很有道理!所以解决这个问题的一个简单方法是:

public IQueryable<BvIndexRow> GetBenefitVariableIndex(int healthPlanId)
{
   var q = (... your query ...
      select new BvIndexRow
      {
         Type = (bv.Private.ToLower() == "y" ? "Private " : "Public ") + (bao.ba_system_variable_ind ? "System" : "Benefit"),
         Class = bv.Variable_Classification,
         ServiceType = bvst.Service_Type.Service_Type_Name ?? string.Empty,
         LineOfBusiness = bv.LOB,
         Status = bv.Status.ToLower() == "p" ? "Production" : "Test",
         Name = bao.ba_object_Code,
         Description = bao.ba_Object_Desc,
         Id = bv.Variable_Id,
      }).ToArray();

   foreach (var bvIndexRow in q) {
      bvIndexRow.ValidCodes = GetValidCodes(bvIndexRow .Variable_Id);
   }

   return q;
}

这应该给你想要的!

编辑:顺便说一句,您可能还想在 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:

public IQueryable<BvIndexRow> GetBenefitVariableIndex(int healthPlanId)
{
   var q = (... your query ...
      select new BvIndexRow
      {
         Type = (bv.Private.ToLower() == "y" ? "Private " : "Public ") + (bao.ba_system_variable_ind ? "System" : "Benefit"),
         Class = bv.Variable_Classification,
         ServiceType = bvst.Service_Type.Service_Type_Name ?? string.Empty,
         LineOfBusiness = bv.LOB,
         Status = bv.Status.ToLower() == "p" ? "Production" : "Test",
         Name = bao.ba_object_Code,
         Description = bao.ba_Object_Desc,
         Id = bv.Variable_Id,
      }).ToArray();

   foreach (var bvIndexRow in q) {
      bvIndexRow.ValidCodes = GetValidCodes(bvIndexRow .Variable_Id);
   }

   return q;
}

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!

无名指的心愿 2024-12-15 01:24:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文