计算类似id的分数之和 [英] Calculating sum of the scores of similar ids

查看:85
本文介绍了计算类似id的分数之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我正在使用C#语言在asp.net中创建一个网站。



我有9个清单。每个列表都有2列:ID为int类型,Value为int16类型。 ID是唯一的。



我想用特殊公式计算每个列表中每个ID的分数。例如,您认为这是计算所有分数后的结果:



 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
...


我想计算每个id的分数总和。我的意思是,在我的例子中,身份证号码100的最终得分是:10 + 10.5 + ....



你建议我做什么?我的第一阶段的想法是创建一个列表和存储ID,列表名称,值和分数(如上表),但我不知道如何做第2阶段。



如果你能帮助我,我将不胜感激。

非常感谢



代码块添加 - OriginalGriff [/ edit]

解决方案

如果你创建一个列表,那么你就在那里。我要做的是进一步前进,创建一个类来保存值,然后构建一个类实例列表:

  public   class  UserScore 
{
public int ID { get ; set ; }
public int Number { get ; set ; }
public int 值{ get ; set ; }
public double 得分{ get ; set ; }
public UserScore( int id, int number, int value double 得分)
{
ID = id;
数量=数字;
Value = value ;
得分=得分;
}
}

然后你可以构建一个类的List并填充它:

 List< userscore> list =  new 列表< 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 ));

你可以然后使用Linq对结果进行分组:

  var  totals = 来自 uc  列表
group uc by uc.ID into g
选择 new {ID = g.Key,TotalScore = g.Sum(x = > x.Score)};







删除了伪造的XML [/ edit]


如果你使用sql然后使用SUM()和group by子句

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]

解决方案

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:

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:

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:

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]


if you are using sql then use SUM() with group by clause.


这篇关于计算类似id的分数之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆