Getting percentages.

Posted by user287798 on Stack Overflow See other posts from Stack Overflow or by user287798
Published on 2010-03-09T10:01:39Z Indexed on 2010/03/09 10:06 UTC
Read the original article Hit count: 210

Filed under:

Hi, i have got a standing answer in this thread. http://stackoverflow.com/questions/2396203/get-the-count-of-elements-where-candidate-has-won

But i am failing to get the percentage, what am i doing wrong. I have the code below.

var s4 = from can in allCandidates

        let noDists= ((from d in root.Elements("Provinces") 
                       select d.Attribute("Province").Value).Distinct()).Count()    

        let count = (from winner in

                     (from p in root.Descendants("Province_Data")

                      let maxVotes = (from c in p.Elements("Candidate")
                                      select c)
                      .Max(x => ((int)x.Element("votes")))

                      select (from c in p.Elements("Candidate")
                              select c).Where(x => ((int)x.Element("votes")) == maxVotes)
                      .First().Element("name").Value )

                     where winner == can
                     select winner).Count()

        orderby count descending

        select new { Candidate = can, NumberOfProvincesWon = count,Percentage= (count/noDists)*100};

    foreach (var d in s4)        
        Console.WriteLine("  {0}", d.ToString());

© Stack Overflow or respective owner

Related posts about linq-to-xml