【问题标题】:LINQ to Entities does not recognize the method Double Round(Double, Int32, System.MidpointRounding) methodLINQ to Entities 无法识别方法 Double Round(Double, Int32, System.MidpointRounding) 方法
【发布时间】:2014-09-19 12:23:36
【问题描述】:

我已经在 Linqer 中尝试了以下 LINQ 查询,它工作正常,但是当我尝试使用 C# 时它给出了以下错误

from IHeal_Mnt_Tickets in iHealEntities.iHeal_Mnt_Tickets
    where
        Tickets.Active == 1 &&
        Tickets.MntID == 1 &&
        Tickets.InsertedOn >= fromdate && 
        Mnt_Tickets.InsertedOn <= todate &&
        (new string[] { "Resolved", "Assigned" }).Contains(Tickets.status)
        group Tickets by new {
            Tickets.Instance
        } into g
            select new {
              Instance = g.Key.Summus_Instance,
              Assigned = (Int64?)g.Count(p => p.iHealID != null),
              resolved = (System.Int64?)g.Sum(p => (p.status == "Resolved" ? 1 : 0)),
              domain = (System.Int64?)g.Sum(p => (p.status == "Assigned" ? 1 : 0)),
              iHeal_Closure = (Decimal?)Math.Round((Double)(Double)g.Sum(p => (p.iHeal_Cur_status == "Resolved" ? 1 : 0)) * 1.0 / (Double)g.Count(p => p.iHealID != null) * 100, 2, MidpointRounding.AwayFromZero)
            };

错误是

"LINQ to Entities does not recognize the method 'Double Round(Double, Int32, System.MidpointRounding)' method, and this method cannot be translated into a store expression."

【问题讨论】:

  • MidpointRounding.AwayFromZero 在 LINQ 查询中不受支持

标签: linq double rounding linqer


【解决方案1】:

并非 BCL 中支持的所有内容在 SQL 中都具有直接等效项。鉴于这是查询的最后一部分,最简单的方法是编写一个查询,该查询获取您需要的所有数据而不四舍五入等,然后使用本地查询:

var dbQuery = from item in source
              where filter
              select projection;
// The AsEnumerable() part is key here
var localQuery = from item in dbQuery.AsEnumerable()
                 select complicatedTransformation;

使用AsEnumerable() 实际上只是更改了编译时类型...因此Select 调用是使用委托的Enumerable.Select,而不是使用表达式树的Queryable.Select

我希望你可以使最终的转换比你当前的方法简单得多 - 像 (Double)(Double) 这样的东西真的没有必要......并且任何时候你从 double 转换decimal 或反之亦然,您应该质疑这是否有必要或可取...通常最好坚持 either 二进制浮点 十进制浮点,而不是混合它们。

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2015-09-20
    相关资源
    最近更新 更多