LINQ - join with Group By and get average
SQL:
SELECT DeptId,avg(Marks) FROM StudentTb s
JOIN StudInDepartment d on s.StudentId = 开发者_StackOverflowd.StudentId
GROUP BY DeptId
Convert into Linq:
var deptRpts = from s in this.ObjectContext.StudentTb
join d in this.ObjectContext.StudInDepartment on s.StudentId equals d.StudentId
group s by d.DeptId into grp
select new {
DeptId = grp.Key,
AverageMarks = grp.Average(ed=>ed.Marks)
};
Got an empty result list.
While expanding the result-set in debug mode.Its shows error function evaluation timed out
Need help on this.
The query works fine as is, at least in my mock up I just threw together ... it may have something to do with how the database is structured....in my mock up I have the studentID as a foreign key to the StudInDepartment (ha!) table and even this simplified query works ok
var deptRpt2 = from d in ctx.StudInDepartment
group d by d.DeptId into grp
select new {
Dept = grp.Key,
AverageMarks = grp.Average(ed=>ed.StudentTb.Marks)
};
The message you received "function evaluation timed out" may be the Visual Studio debugger issue, can't help you there, theres other threads off stackoverflow that discuss it as well.
精彩评论