【问题标题】:LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression [duplicate]LINQ to Entities 无法识别“Double ToDouble(System.String)”方法,并且该方法无法转换为存储表达式 [重复]
【发布时间】:2018-09-28 03:19:50
【问题描述】:

我目前正在将一些老程序员从旧系统中的存储过程转换为 linq,但我不知道是什么导致了这个错误。代码如下:

控制器:

var ObligationRequestDetailsTotalCostByOoe = (from p in db.iBudget_ObligationRequestDetails
where p.ooe_general_id != null && p.is_approved == 1
group p by p.ooe_general_id into g
select new
{
id = g.Key,
amount = g.Sum(p => Convert.ToDouble(p.amount))
}).ToList();

型号:

public int id { get; set; }
public string amount { get; set; }

我尝试将 Convert.ToDouble() 更改为 Double.Parse() 但此错误 'Single Parse(System.String)' 显示。 我尝试在 p.amount 之后添加 'System.Globalization.CultureInfo.InvariantCulture' 但错误仍然存​​在。

我在这里做错了什么?提前致谢。

【问题讨论】:

  • EF 正在尝试将表达式转换为 SQL,但没有等效项。
  • @Nkosi 我可以在这里做什么转身?我试图在这里做提示 = c-sharpcorner.com/blogs/… 但惨败
  • @mjwills p.amount 是上面列出的模型 iBudget_ObligationRequestDetails 中的一个值,一个字符串变量
  • 分组前拆分查询,使用AsEnumerable,之后应该可以使用convert或parse方法
  • 如果是数量为什么是字符串类型?是否可以将 DB(和 C#)更改为不同的数据类型?

标签: c# linq


【解决方案1】:

您将需要使用AsEnumerable 以便在从数据库返回后处理内存中的记录。然后你就可以使用Convert.ToDouble

var ObligationRequestDetailsTotalCostByOoe = (from p in db.iBudget_ObligationRequestDetails
where p.ooe_general_id != null && p.is_approved == 1
group p by p.ooe_general_id into g
select g) // <-- db call
.AsEnumerable() //<-- memory call
.Select(g => new {
    id = g.Key,
    amount = g.Sum(p => Convert.ToDouble(p.amount))
})
.ToList();

【讨论】:

  • 哦。有效!谢谢你。所以我做错了 .AsEnumerable()
猜你喜欢
  • 2011-08-23
  • 2017-09-08
  • 1970-01-01
  • 2012-04-21
相关资源
最近更新 更多