Linq to Sql Left join and get Average
Hi ,Let's say i have an Linq query :
var query = from course in _unitOfWork.Course.GetAll()
join candidate in _unitOfWork.CandidateCourses.GetAll() on course.Id equals candidate.Course_id
join cr in _unitOfWork.CourseReviews.GetAll() on course.Id equals cr.Course_id into g from rt in g.DefaultIfEmpty()
where candidate.UserId == CandidateId
select new CourseFields
{
Id = course.Id,
Course_name = course.Course_name,
Course_description = course.Course_description,
Rating = g.Average(x => x.Rating),
TotalSections = sections.Count(),
};
return query;
Here i am getting exception in calculating Average of Rating, When there is no data in table for that particular course.
Can someone help where i am doing wrong..