gpt4 book ai didi

c# - linq 中不允许使用 String.Format 或 toString

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:04 24 4
gpt4 key购买 nike

下面的代码不被喜欢并抛出以下错误。

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

在我将运输方式的费用从十进制更改之前,我能够做到这一点吗?到十进制。我把它改成了十进制,它开始抛出一个错误,然后我把它改回来,现在它仍然不喜欢这样。这里做错了什么。我只是想使文本成为运输方式名称与 - 的连接,并将成本作为文本。

  var ShippingMethods = db.ShippingMethods.Select(x => new SelectListItem()
{
Text = x.Name + " - " + String.Format("c", x.Cost),
Value = x.Cost.ToString()
}).ToList();

最佳答案

你必须使用 .AsEnumerable()以及你可以从here中找到的原因

 db.ShippingMethods.AsEnumerable().Select(x => new SelectListItem()
{
Text = x.Name + " - " + String.Format("c", x.Cost),
Value = x.Cost.ToString()
}).ToList();

关于c# - linq 中不允许使用 String.Format 或 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427494/

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