1
votes

I was suggested from a friend the following code

var tmp = from allCourses in _db.tblCourseNeededHours orderby allCourses.tblCourse.CourseName where allCourses.Semester == semester select allCourses;

return tmp.Sum(x => x.NeededHoursPerWeek);

but i am receving this error

Cannot implicitly convert type 'double' to 'System.Linq.IQueryable

Any idea how to fix it?

enter image description here

public IQueryable FindSumAllCourseNeededHoursInSemester(string semester)

2
Based on your updates, I'm not sure why you need to use the grouping. If each of the first two rows in your HTML table represents a record in your database table, I see no reason to group. You just seem to want the sum of everything in that table. - Chris Farmer

2 Answers

5
votes

How about this:

var sum = FindAllCourseNeededHoursInSemester(mySemester).Sum(course => course.NeededHours)

This MSDN article has a good primer on some common LINQ tasks:

http://code.msdn.microsoft.com/windowsdesktop/101-LINQ-Samples-3fb9811b

2
votes

Chris is correct if you want the complete total, if you want it by course name use GroupBy:

var hours = FindAllCourseNeededHoursInSemester(mySemester)
    .GroupBy(g => g.CourseName)
    .Select(c => new { c.Key, c.Sum(a => a.NeededHours) } );