2

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.

1
  • Entity Framework still doesn't support this? That's unfortunate. LINQ to SQL allows this from day one.
    – Steven
    Oct 3, 2012 at 14:52

4 Answers 4

7

You will simply need to avoid executing that method on the database side; get what you need from the database, convert your query to a linq-to-objects query, and then call your method.

var categories = _db.Categories.Select(x => new {
        ID = x.ID,
        Name = x.NameLatvian,
        ItemCount = x.Items.Count
    })//restrict the columns returned from the db
    .AsEnumerable()//Switch to Linq-to-objects
    .Select(x => new {
        x.ID,
        Breadcrumbs = Infrastructure.CategoryHelpers.Breadcrumbs(x.ID, -1, ""),
        x.Name,
        x.ItemCount,
    });
2
  • AsEnumerable? should be ToList()?
    – cuongle
    Oct 3, 2012 at 19:01
  • @CuongLe No, it most certainly should not be. ToList would be wrong here; it would have a significant negative impact on performance for absolutely no gain.
    – Servy
    Oct 3, 2012 at 19:02
2

You should be able to use AsEnumerable() which makes Linq evaluate the rest of the expression locally;

var categories = _db.Categories.Select(x => new { 
    ID = x.ID,                                    // This select is done in DB
    Name = x.Name,
    ItemCount = x.Items.Count
})
.AsEnumerable()                                   // Get result to LINQ to Objects                                   
.Select(x => new {                 
    ID = x.ID,                                    // This select is done in memory.
    Breadcrumbs = Infrastructure.CategoryHelpers.Breadcrumbs(x.ID, -1, ""),
    Name = x.Name,
    ItemCount = x.ItemCount
});

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.

3
  • Note that here the entire query becomes a linq-to-objects query, this means that you're not restricting the columns returned from the database; you're grabbing them all. If you don't have that many more then it's fine, but if there are a lot of columns not being returned here then you'll want to pull those columns back before calling AsEnumerable().
    – Servy
    Oct 3, 2012 at 14:41
  • @Servy Agreed, also you usually want a Where expression to filter your fetch, that should be placed before AsEnumerable(). Oct 3, 2012 at 14:42
  • If you don't actually need all of the rows, yes.
    – Servy
    Oct 3, 2012 at 14:43
0

A Linq to Entities query cannot invoke a method in your application for each returned row (it cannot call Breadcrumbs).

The best answer I know of is to return x.ID and separately call Breadcrumbs() in the application layer, e.g. by using .AsEnumerable() to iterate the results in code.

0
var categories = from i in (from c in _db.Categories
                            select new
                            {
                                ID = c.ID,
                                Name = c.Name,
                                ItemCount = c.Items.Count
                            }).ToList()
                 select new
                 {
                     ID = i.ID,
                     Name = i.Name,
                     ItemCount = i.ItemCount,
                     Breadcrumbs = Infrastructure.CategoryHelpers.Breadcrumbs(c.ID, -1, String.Empty)
                 };

But please see comments below.

Model Defined Functions

You might also investigate model defined functions if you're using an EDMX file rather than code first.

1
  • 1
    You don't want to be calling ToList here, especially if there are a large number of rows returned. It will force the entire set of data to be drawn into memory. AsEnumerable will allow you to switch to linq-to-objects while still streaming the data.
    – Servy
    Oct 3, 2012 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.