c# - Error in Linq, Count() -


i'm trying write sql query linq:

sql:

select c.course_name, count(s.s_name) studenti course c  join study_group g on g.course_id=c.id  join student s on s.study_group_id=g.id  group c.course_name; 

linq:

var countstudents = (from s in ado.student //on g.id equals s.study_group_id                      join g in ado.study_group on s.study_group_id equals g.id                      join c in ado.course on g.course_id equals c.id                      group s c.course_name cn                      let count = cn.count(co => co.s_name)                      select new                       {                         c.course_name                         course_name = cn.key                      }); 

and still have error @ co => co.s_name

error: cannot implicitly convert type 'string' 'bool'

know how fix ?

thank you.

the sql count(column) aggregate function counts not null values. equivalent in linq replace line:

let count = cn.count(co => co.s_name)   

by

let count = cn.count(co => co.s_name != null)   

of course, no guarantees on generated sql here. either way, strange student's name may null, though have no intention of discussing model.

note
won't able retrieve desired count unless select in code. also, check if students' names can null, because in case can't, cn.count() suffice.


Comments