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";
}
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!