Thursday 3 May 2012

"Case when" in Linq



using (EntityModel obj = new EntityModel())
            {
                var query = from grade in obj.StudentGrade
                                join student in obj.Person on grade.Person.PersonID
                                equals student.PersonID
                                select new
                               {
                                  FirstName = student.FirstName,
                                  LastName = student.LastName,
                                  Grade = grade.Grade.Value >= 4 ? "A" :
                                             grade.Grade.Value >= 3 ? "B" :
                                             grade.Grade.Value >= 2 ? "C" :
                                             grade.Grade.Value != null ? "D" : "-"
                               };
 
                foreach (var q in query)
                {
                    Console.WriteLine("{0} {1} has {2} grade", q.FirstName,
                                              q.LastName, q.Grade);
                }
                Console.Read();
            } 

Enjoy............

No comments:

Post a Comment