LINQ to Entities无法识别方法Double Round(Double,Int32,System.MidpointRounding)方法

问题描述 投票:1回答:1

我在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."
linq double rounding linqer
1个回答
6
投票

并非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,反之亦然,你都应该质疑这是必要的还是可取的...通常最好坚持二进制浮点或十进制浮点,而不是混合它们。

© www.soinside.com 2019 - 2024. All rights reserved.