LINQ到实体无法识别方法双轮(双人间,的Int32,System.MidpointRounding

2019-10-21 00:40发布

我试过下面的LINQ查询在Linqer它工作正常,但它给下面的错误,而我用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."

Answer 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)真的是没有必要的......你从转换任何时候doubledecimal或反之,你应该质疑不管是必要或适宜...它通常是更好地坚持以二进制浮点十进制浮点,而不是将它们混合。



文章来源: LINQ to Entities does not recognize the method Double Round(Double, Int32, System.MidpointRounding) method