Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I'm creating a web site in asp.net with C# language.

I have 9 lists. Each lists have 2 columns : ID with int type, and Value with int16 type. ID is unique.

I want to calculate score of each ID in each list with a special formula. For instance, you think this is the result after calculating all scores:

id    list number   value   score
100   1              2      10
300   1              2      9
200   1              4      11
100   2              3      10.5
200   2              3      10
...

I want to calculate sum of the scores of each id. I mean, the final score of id number 100 in my example is: 10+10.5+....

What do you suggest for me to do this? My idea for first stage is to create a list and store id, list name, value, and score in it(like the above table), but I don't know how to do stage 2.

I would be grateful if you kindly help me.
Thanks a lot

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Sep-14 4:12am
v2

If you create a list then you are half way there. What I would do is go one stage further, and create a class to hold the values, and then build a list of the class instances:
C#
public class UserScore
   {
   public int ID { get; set; }
   public int Number { get; set; }
   public int Value { get; set; }
   public double Score { get; set; }
   public UserScore(int id, int number, int value, double score)
      {
      ID = id;
      Number = number;
      Value = value;
      Score = score;
      }
   }
You can then construct a List of the class and fill it:
C#
List<userscore> list = new List<userscore>();
list.Add(new UserScore(100, 1, 2, 10));
list.Add(new UserScore(300, 1, 2, 9));
list.Add(new UserScore(200, 1, 4, 11));
list.Add(new UserScore(100, 2, 3, 10.5));
list.Add(new UserScore(200, 2, 4, 10));
You can then use Linq to group the results:
C#
var totals = from uc in list
             group uc by uc.ID into g
             select new { ID = g.Key, TotalScore = g.Sum(x => x.Score) };



[edit]Spurious XML removed[/edit]
 
Share this answer
 
v2
Comments
k5_ce 22-Sep-14 9:59am    
Thanks for your answer. It really helped me.
Best Regards.
if you are using sql then use SUM() with group by clause.
 
Share this answer
 
Comments
k5_ce 22-Sep-14 9:54am    
Thanks for your answer. These data are not saved in database. The raw data are stored in database. I put them to several formulas, and after that I want to do this stages.
Best regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900