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

1 linq double rounding linqer

我在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)
            };
Run Code Online (Sandbox Code Playgroud)

错误是

"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."
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 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;
Run Code Online (Sandbox Code Playgroud)

AsEnumerable()有效地使用只是更改编译时类型...以便Select调用Enumerable.Select使用委托而不是Queryable.Select使用表达式树.

我希望你能作出最后的改造很多比你现在的做法简单,但-之类的东西(Double)(Double)实在是没有必要的......你从转换任何时间doubledecimal,反之亦然,你应该质疑这是否是必要或适宜...它通常是更好地坚持要么二进制浮点十进制浮点,而不是将它们混合.