LINQ to Entities无法识别Double Double(Double,Int32,System.MidpointRounding)方法 [英] LINQ to Entities does not recognize the method Double Round(Double, Int32, System.MidpointRounding) method

查看:97
本文介绍了LINQ to Entities无法识别Double Double(Double,Int32,System.MidpointRounding)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried the below LINQ Query in Linqer it is working fine but it is giving the below error while i tried with 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."

推荐答案

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

Not everything that's supported in the BCL has a direct equivalent in SQL. Given that this is the final part of the query, the simplest approach would be to just write a query which fetched all the data you need without rounding etc, and then transform that data into your preferred format using a local query:

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.

Using AsEnumerable() effectively just changes the compile-time type... so that the Select call is Enumerable.Select using a delegate rather than Queryable.Select using an expression tree.

我希望您可以使最终转换 比您当前的方法简单得多-确实不需要像(Double)(Double)这样的东西……而且每次从double转换时到decimal或反之亦然,您应该质疑这是必要还是可取的...通常最好还是使用 二进制浮点数十进制浮点数,而不是混合它们.

I would hope that you could make the final transformation much simpler than your current approach though - things like (Double)(Double) really aren't necessary... and any time you convert from double to decimal or vice versa, you should question whether that's necessary or desirable... it's generally better to stick to either binary floating point or decimal floating point, rather than mixing them.

这篇关于LINQ to Entities无法识别Double Double(Double,Int32,System.MidpointRounding)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆