Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I has same a problem. which I want caculator score for student. I done:
C#
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 
                           };



I want to add column FinalMatch and assign
C#
ssh.Midterm * 0.2 + ssh.Project * 0.3 + ssh.Endterm * 0.5 
, But I don't get any result
Posted

Strange! I just knocked together something similar:
C#
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
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?
 
Share this answer
 
Comments
Amir Mahfoozi 23-Oct-11 5:38am    
+5
williamVu 23-Oct-11 9:05am    
Thanks OriginalGriff,
When I said "But I don't get any result", I mean, I don't get any result for "FinalMatch", although other value in the same row is has result.
In here, I has select from two table: [Student],[ScoreSheet] all table have recors. I want to enter score for student and caculater fina match.
Forward to receiving your solutions!
Respectfully william!
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.

C#
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) };
 
Share this answer
 
v3
Comments
williamVu 23-Oct-11 11:09am    
thanks you so much, this good solve my problem!
Bala Selvanayagam 23-Oct-11 15:22pm    
Vote and accept the answer, if you are happy with

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