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)'

查看:29
本文介绍了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
                   };
}

虽然有问题.我正在尝试创建任务.对于每个新任务,当我将链接文本设置为像Hello"这样的常量字符串时,这很好.但是在上面,我正在尝试使用发票的属性构建属性链接文本.

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?

推荐答案

Entity Framework 正在尝试在 SQL 端执行您的投影,其中没有等效于 string.Format.使用 AsEnumerable() 强制评估Linq to Objects 的那部分.

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天全站免登陆