Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC中的Linq百分比_C#_Linq To Sql - Fatal编程技术网

C# MVC中的Linq百分比

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 我需要划分: 百分比=计数/总数*

我将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
我需要划分:

百分比=计数/总数*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不支持银行家轮,这是默认的四舍五入。