C# MVC中的Linq百分比
我将MVC3与c结合使用,并试图从我的模型中获得以下百分比: 我检索数字:C# MVC中的Linq百分比,c#,linq-to-sql,C#,Linq To Sql,我将MVC3与c结合使用,并试图从我的模型中获得以下百分比: 我检索数字: ... Code omitted AgeGroup = g.Key.AgeGroup, Count = (int)g.Count(), Total = (int) (from vw_masterview0 in ctx.vw_MasterViews select new { vw_masterview0.ClientID }).Count() ... Code omitted 我需要划分: 百分比=计数/总数*
... Code omitted
AgeGroup = g.Key.AgeGroup,
Count = (int)g.Count(),
Total = (int)
(from vw_masterview0 in ctx.vw_MasterViews
select new
{
vw_masterview0.ClientID
}).Count()
... Code omitted
我需要划分:
百分比=计数/总数*100
我不知道如何在Linq中格式化它。首先需要转换为十进制或双精度,以避免整数除法。在与100相乘并取整后,您需要返回int
另一方面,将计数转换为int是无用的,Count已经返回一个整数
int count = g.Count();
int total = ctx.vw_MasterViews.Count();
int percent = (int)Math.Round((Decimal)count/(Decimal)total*100, MidpointRounding.AwayFromZero);
首先需要转换为十进制或双精度,以避免整数除法。在与100相乘并取整后,您需要返回int
另一方面,将计数转换为int是无用的,Count已经返回一个整数
int count = g.Count();
int total = ctx.vw_MasterViews.Count();
int percent = (int)Math.Round((Decimal)count/(Decimal)total*100, MidpointRounding.AwayFromZero);
谢谢,现在我得到了这个错误:为了转换为SQL,Math.Round方法需要一个中点舍入参数。使用“AwayFromZero”指定SQL函数轮,然后按其说明执行。我已更新我的答案,以包含舍入模式。我怀疑这是因为SQL不支持银行家舍入,这是默认舍入。谢谢,现在我得到了这个错误:对于转换为SQL,Math.round方法需要一个中点舍入参数。使用“AwayFromZero”指定SQL函数轮,然后按其说明执行。我已更新我的答案,以包含舍入模式。我怀疑发生这种情况是因为SQL不支持银行家轮,这是默认的四舍五入。