【问题标题】:LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression [duplicate]LINQ to Entities 无法识别“System.String get_Item(Int32)”方法,并且该方法无法转换为存储表达式 [重复]
【发布时间】:2017-04-08 21:47:32
【问题描述】:

我正在尝试使用 linq 查询从数据库中获取价格,但此异常不断出现,我不知道为什么。 数据类型相同,都是小数。

newOrder.Cmimi = Convert.ToDecimal(context.Produktets.Where(c=>c.Emri_Produktit == prodName[0]).Select(c => c.Cmimi).First());

有人可以帮忙吗?????

【问题讨论】:

  • prodName[0] 分配给查询外部的变量并在内部使用该变量。

标签: c# database linq exception


【解决方案1】:

看起来您在 linq 查询中使用数组索引器,而 linq to entity 无法将 prodName[0] 转换为 SQL。您可以尝试将值存储在变量中并在查询中使用它:

var name = prodName[0];
newOrder.Cmimi = Convert.ToDecimal(context.Produktets
                                          .Where(c => c.Emri_Produktit == name)
                                          .Select(c => c.Cmimi).First());

【讨论】:

  • 这不仅适用于数组,而且适用于每个带有索引器的数据结构(例如列表)
猜你喜欢
  • 2016-12-19
  • 2017-09-08
  • 2012-12-02
  • 2014-06-06
  • 1970-01-01
  • 2022-06-22
相关资源
最近更新 更多