Reputation: 2502
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";
}
Upvotes: 1
Views: 286
Reputation: 2554
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!
Upvotes: 2
Reputation: 160942
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.
Upvotes: 2