本文介绍了我如何使用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
推荐答案
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?
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进行多个属性的求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!