【发布时间】:2014-01-30 22:02:42
【问题描述】:
我在 C# 中进行 LinQ 查询,但我收到此错误:
System.Data.Entity.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理
附加信息:LINQ to 中不支持强制转换为十进制 实体查询,因为需要精度和比例信息 无法推断。
最近我在我的Lightswitch应用程序中创建了一个RIA服务,我想要一个简单的操作。
public class Reporte_HorasporCliente
{
[Key]
public int ID { get; set; }
public string Cliente { get; set; }
public double HPD { get; set; }
public double? HPG { get; set; }
public double? HAP { get; set; }
public double? HCL { get; set; }
public double? HEP { get; set; }
public double? HEJ { get; set; }
public double? HFT { get; set; }
public double? HPP { get; set; }
public double? Saldo { get; set; }
public decimal? Cumplimiento { get; set; }
public DateTime FechaOrden { get; set; }
}
public IQueryable<Reporte_HorasporCliente> getReporteHrsCliente()
{
var query = from q in this.Context.DetalleOrdenCompras
select new Reporte_HorasporCliente
{
ID = q.Id,
Cliente = q.OrdenCompra.Cliente.Nombre,
HAP = q.HAP,
HCL = q.HCL,
HEJ = q.HEJ,
HEP = q.HEP,
HFT = q.HFT,
HPD = q.HPD,
HPG = q.HPG,
HPP = q.HPD - q.HPG,
Saldo = q.HPD - q.HEJ,
FechaOrden = q.OrdenCompra.FechaOrden,
Cumplimiento = (q.HEJ == 0 ? 0 : ((q.HPD / q.HEJ) * 10) / 100),
};
return query;
}
HEJ 和 HPD 是双倍的。
在“Cumplimiento”中我正在做:
Cumplimiento = (decimal)(q.HEJ == 0 ? 0 : ((q.HPD / q.HEJ) * 10) / 100),
Cumplimiento = Convert.ToDecimal((q.HEJ == 0 ? 0 : ((q.HPD / q.HEJ) * 10) / 100)),
Cumplimiento = (q.HEJ == 0 ? 0 : (((decimal)q.HPD / (decimal)q.HEJ) * 10) / 100),
没有任何结果,我真的需要一些帮助,谢谢。
【问题讨论】:
-
附注:在双精度和十进制之间转换(任一方向)通常是个坏主意。这表明一方或另一方选择了不合适的数据类型。
-
甚至没有接近最佳解决方案,但如果你现在真的需要它,我建议你尝试 Cumplimiento = Decimal.Parse(q.HEJ == 0 ? 0 : ((q.HPD / q.HEJ) * 10) / 100).ToString())
-
对不起,我错过了一个括号。你去: Cumplimiento = Decimal.Parse((q.HEJ == 0 ? 0 : ((q.HPD / q.HEJ) * 10) / 100).ToString())
-
同样的错误“System.Data.Entity.dll 中发生类型为‘System.NotSupportedException’的异常,但未在用户代码中处理附加信息:LINQ to Entities 无法识别方法‘System .Decimal Parse(System.String)' 方法,并且该方法不能翻译成存储表达式。"
标签: linq silverlight service ria