我已经在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."
答案 0 :(得分:6)
并非BCL中支持的所有内容都在SQL中具有直接等效功能。鉴于这是查询的最后一部分,最简单的方法是编写一个查询,该查询获取所需的所有数据,无需舍入等,然后使用a将数据转换为首选格式本地查询:
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
到{{1}}或反之亦然,你应该质疑这是必要的还是可取的...通常更好地坚持 二进制文件浮点或十进制浮点数,而不是混合它们。