我如何使用Linq进行多个属性的求和 [英] How can I multiple and sum some properties using Linq

查看:76
本文介绍了我如何使用Linq进行多个属性的求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的问题.我想为学生计算分数.我做了:

I has same a problem. which I want caculator score for student. I done:

var cosrse = from ssh in cn.DB.ScoreSheets 
                            where ssh.CourseID == CourseID 
                            select new 
                           { 
                              Student = ssh.Student.LastName +" "+ ssh.Student.FirstName, 
                              ssh.Student.StudentID, Assignment = ssh.Assignment,
                              Project = ssh.Project,
                              ssh.Midterm,
                              ssh.Endterm,
                              ssh.Practice,
                              FinalMatch = ssh.Midterm * 0.2 + ssh.Project * 0.3 + ssh.Endterm * 0.5 //  new a propety FinalMatch 
                           };




我想添加列 FinalMatch 并分配




I want to add column FinalMatch and assign

ssh.Midterm * 0.2 + ssh.Project * 0.3 + ssh.Endterm * 0.5 

,但我没有得到任何结果

, But I don''t get any result

推荐答案

奇怪!我只是敲了类似的东西:
Strange! I just knocked together something similar:
private void button1_Click(object sender, EventArgs e)
        {
        List<Student> list = new List<Student>();
        list.Add(new Student(10, 11));
        list.Add(new Student(20, 21));
        list.Add(new Student(30, 31));
        list.Add(new Student(40, 41));
        var c = from s in list
                select new { A = s.A, B = s.B, sum = s.A + s.B };
        foreach (var s in c)
            {
            Console.WriteLine("{0}:{1}+{2}", s.sum, s.A, s.B);
            }
        }
    }
public class Student
    {
    public int A { get; set; }
    public int B { get; set; }
    public Student(int a, int b)
        {
        A = a;
        B = b;
        }
    }

而我得到了期望的结果:

And I get what I would expect:

21:10+11
41:20+21
61:30+31
81:40+41

因此,当您说:但是我没有任何结果"时,您会得到什么?可能是您的ssh.CourseID == CourseID条件没有选择任何记录吗?

So when you say: "But I don''t get any result" what do you get? Could it be that your ssh.CourseID == CourseID condition is selecting no records?


如果数据库中的任何Midterm OR Project OR Endterm为空,都可能会发生这种情况,

任何涉及null的数学加法都将得出null,例如10 + null将为Null,这可能是您未获得任何结果(null)的原因,

您需要处理null并将其设置为零(如果在类似于以下查询的方程式中为null).

This can happen, if any of your Midterm OR Project OR Endterm is null in the database,

Any mathematical addition involving null will result null, for example 10+null will be Null and that could be the reason you do not get any results (null),

you needs to handled null and make it zero, if it is null in the equation similar to the following query.

var result = from c in db.scoresheets
                        where c.CourseID == 1
                        select new { Mid = c.MidTerm, Last = c.LastTerm, sum1 =( c.MidTerm == null ? 0 : c.MidTerm )+ (c.LastTerm == null ? 0 : c.LastTerm) };


这篇关于我如何使用Linq进行多个属性的求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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