简体   繁体   English

如何在LINQ查询中运行此函数

[英]How can I run this function inside LINQ query

I have function that will generate breadcrumbs format for my category eg Root->Children . 我有功能,将为我的类别生成面包屑格式,例如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. 但是我无法将其转换为LINQ查询。

Can someone explain how can I cast this particular function inside LINQ query? 有人可以解释我如何在LINQ查询中转换这个特定的函数? I've tried getting data and then setting it in foreach loop but it said that property is read only. 我已经尝试获取数据,然后在foreach循环中设置它,但它说该属性是只读的。

Function is Infrastructure.CategoryHelpers.Breadcrumbs({id}) it will return string . 函数是Infrastructure.CategoryHelpers.Breadcrumbs({id})它将返回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. LINQ to Entities无法识别方法'System.String Breadcrumbs(Int32,Int32,System.String)'方法,并且此方法无法转换为商店表达式。

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. 从数据库中获取所需内容,将查询转换为linq-to-objects查询,然后调用方法。

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,
    });

You should be able to use AsEnumerable() which makes Linq evaluate the rest of the expression locally; 您应该能够使用AsEnumerable() ,这使得Linq在本地评估表达式的其余部分;

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. 请注意,您需要在AsEnumerable()调用之前完成数据库所需的所有过滤(columns / Where表达式/ GroupBy / OrderBy),因为该调用将获取之前设置的内容到本地IEnumerable,并且使用Linq to Objects进行的其余操作。

A Linq to Entities query cannot invoke a method in your application for each returned row (it cannot call Breadcrumbs). Linq to Entities查询无法为每个返回的行调用应用程序中的方法(它不能调用Breadcrumbs)。

The best answer I know of is to return x.ID and separately call Breadcrumbs() in the application layer, eg by using .AsEnumerable() to iterate the results in code. 我知道的最好答案是返回x.ID并在应用程序层中单独调用Breadcrumbs(),例如使用.AsEnumerable()在代码中迭代结果。

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. 如果您使用的是EDMX文件而不是代码,则还可以研究模型定义的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM