开发者

Percentage in LINQ comes out as zero

开发者 https://www.devze.com 2023-02-13 04:20 出处:网络
I have the following query. Why do I get zero as the value in the percent column? var qPercentage = from q in qCounts

I have the following query. Why do I get zero as the value in the percent column?

var qPercentage = from q in qCounts
                          select new {
       开发者_如何学C                       q.Category,
                              q.CategoryCouplet,
                              q.Subcategory,
                              Percent =  100*(q.Count / iTotal)
                          };

Counts has valid integer values, by the way!


It looks like you're doing integer division within the parenthesis. Try

100*(q.Count / (double)iTotal)

or if you want Percent to be an integer

(100 * q.Count) / iTotal


Because q.Count and iTotal are integer. You should do 100 * q.Count / iTotal.


Try

Percent =  100*((float)q.Count / iTotal)
0

精彩评论

暂无评论...
验证码 换一张
取 消