I have function that will generate breadcrumbs format for my category e.g. Root->Children. It works because when I use it in my view it does work and doing its job. However I cannot cast it inside LINQ query.
Can someone explain how can I cast this particular function inside LINQ query? I’ve tried getting data and then setting it in foreach loop but it said that property is read only.
Function is Infrastructure.CategoryHelpers.Breadcrumbs({id}) it will return string.
Calling function
public dynamic List()
{
var categories = _db.Categories.Select(x => new {
ID = x.ID,
Breadcrumbs = Infrastructure.CategoryHelpers.Breadcrumbs(x.ID, -1, ""), // this method cannot be translated into a store expression
Name = x.Name,
ItemCount = x.Items.Count
});
foreach (var c in categories)
{
// c.Breadcrumbs = Infrastructure.CategoryHelpers.Breadcrumbs(c.ID); // Value is read only
}
return Json(categories, JsonRequestBehavior.AllowGet);
}
Error
LINQ to Entities does not recognize the method ‘System.String
Breadcrumbs(Int32, Int32, System.String)’ method, and this method
cannot be translated into a store expression.
You should be able to use
AsEnumerable()which makes Linq evaluate the rest of the expression locally;Note that you need to do all filtering (columns/Where expression/GroupBy/OrderBy) that you want done by the database before the
AsEnumerable()call, since that call will fetch what is set up until then to a local IEnumerable, and do the rest of the operations on that using Linq to Objects.