I have one LINQ Query with custom model. I just wanted to use a method to assign a value for a model property. But When I try to use the custom model it throws some error message like this:
LINQ to Entities does not recognize the method ‘System.String GetPONo(Ent, System.String)’ method, and this method cannot be translated into a store expression.
Code
var model = (from p in db.PoDetails
select new porders
{
Category = p.Category,
PONO = GetPONo(p, p.Category),
}).ToList();
Method
public string GetPONo(PoDetail p, string ASD)
{
if (ASD == "B")
{
var PoNo = (from pord in db.Porders where pord.Id == p.PoId select pord.No).FirstOrDefault();
return PoNo;
}
else
{
var PoNo = (from porder in db.Porders
where porder.Id == (from rec in db.RecommendResources where rec.Id == p.BibId select rec.PoId).FirstOrDefault()
select porder.No).FirstOrDefault();
return PoNo;
}
}
You can use ternary operator to do different subquery for PONO depending on category value
Also you can create stored procedure for that.