LINQ to Entities不能识别方法'System.String Format(System.String,System.Object,System.Object)' [英] LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'

查看:77
本文介绍了LINQ to Entities不能识别方法'System.String Format(System.String,System.Object,System.Object)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个linq查询:

I have this linq query:

private void GetReceivedInvoiceTasks(User user, List<Task> tasks)
{
    var areaIds = user.Areas.Select(x => x.AreaId).ToArray();

    var taskList = from i in _db.Invoices
                   join a in _db.Areas on i.AreaId equals a.AreaId
                   where i.Status == InvoiceStatuses.Received && areaIds.Contains(a.AreaId)
                   select new Task {
                       LinkText = string.Format(Invoice {0} has been received from {1}, i.InvoiceNumber, i.Organisation.Name),
                       Link = Views.Edit
                   };
}

它有问题。我正在尝试创建任务。对于每个新的任务,当我将链接文本设置为一个常量字符串,如你好,这很好。但是上面我试图使用发票的属性来构建属性linktext。

It has issues though. I'm trying to create tasks. For each new task when I set the link text to a constant string like "Hello" it is fine. However above I'm trying to build the property linktext using properties of the invoice.

我收到这个错误:

base {System.SystemException} = {LINQ to Entities不识别方法'System.String Format(System.String,System.Object,System.Object)'方法,而这种方法不能翻译成商店表达。}

base {System.SystemException} = {"LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression."}

任何人都知道为什么?任何人都知道这样做的另一种方式使其工作?

Anyone know why? Anyone know an alternative way of doing this to make it work?

推荐答案

实体框架正在尝试在SQL端执行投影,其中没有相当于 字符串。格式 。使用 AsEnumerable() 强制将该部分与Linq对象进行评估。

Entity Framework is trying to execute your projection on the SQL side, where there is no equivalent to string.Format. Use AsEnumerable() to force evaluation of that part with Linq to Objects.

基于我已经给你,我将重构你的查询,如下所示:

Based on the previous answer I have given you I would restructure your query like this:

int statusReceived = (int)InvoiceStatuses.Received;
var areaIds = user.Areas.Select(x=> x.AreaId).ToArray();

var taskList = (from i in _db.Invoices
               where i.Status == statusReceived && areaIds.Contains(i.AreaId)
               select i)
               .AsEnumerable()
               .Select( x => new Task()
               {
                  LinkText = string.Format("Invoice {0} has been received from {1}", x.InvoiceNumber, x.Organisation.Name),
                  Link = Views.Edit
                });

另外我看到你在查询中使用相关实体( Organisation.Name )确保你添加正确的 Include 到你的查询,或者具体实现这些属性以供以后使用,即:

Also I see you use related entities in the query (Organisation.Name) make sure you add the proper Include to your query, or specifically materialize those properties for later use, i.e.:

var taskList = (from i in _db.Invoices
               where i.Status == statusReceived && areaIds.Contains(i.AreaId)
               select new { i.InvoiceNumber, OrganisationName = i.Organisation.Name})
               .AsEnumerable()
               .Select( x => new Task()
               {
                  LinkText = string.Format("Invoice {0} has been received from {1}", x.InvoiceNumber, x.OrganisationName),
                  Link = Views.Edit
                });

这篇关于LINQ to Entities不能识别方法'System.String Format(System.String,System.Object,System.Object)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆