gpt4 book ai didi

LINQ to Entities 无法识别方法 'System.String Format(System.String, System.Object, System.Object)'

转载 作者:行者123 更新时间:2023-12-03 05:13:15 35 4
gpt4 key购买 nike

我有这个 linq 查询:

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”这样的常量字符串时,就可以了。但是,上面我尝试使用发票的属性构建属性链接文本。

我收到此错误:

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."}

有人知道为什么吗?有人知道另一种方法可以使其发挥作用吗?

最佳答案

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

基于on the previous answer我已经给过你,我会像这样重组你的查询:

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

此外,我发现您在查询 (Organization.Name) 中使用了相关实体,请确保将正确的 Include 添加到查询中,或者专门具体化这些属性以供以后使用使用,即:

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)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079990/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com