mysql - Convert SQL Query into C# -
i have following sql query:
select s.name, count(sc.id) classes students s left join studentsclasses sc on s.id = sc.studentid group s.name having count(sc.id) = 0 order count(sc.id);
that query gets count of students classes , returns students least classes.
how can convert c#? attempt not yield expected result. have:
var query = (from students in ent.students join classes in ent.studentsclasses on students.id equals classes.studentid gj subpet in gj.defaultifempty() select new { students.name }).tolist();
however returns names of students registered in courses.
================================
here tables:
================== studentsclasses ---------------- id (registration id of class) studentid (id of student taking class) classid (id of class) ---------------- ================== students --------------- id (id of student) name (name of student) gradelevelid (grade of student) --------------- ==================
final query
var query = (from students in ent.students join classes in ent.studentsclasses on students.id equals classes.studentid gj subpet in gj.defaultifempty() orderby students.studentsclasses.count students.studentsclasses.count == 0 select new { students.name }).tolist();
edit: fixed statement
ienumerable<igrouping<student, student>> query = student in ent.students join studentclass in ent.studentclasses on student.id equals studentclass.studentid studentclassesj studentclass in studentclassesj.defaultifempty() group student student studentgroup select studentgroup; ienumerable<student> result = query.where(x => x.count() == query.min(y => y.count())).select(x => x.key);
be aware although stated sql query return students least umber of classes, sql returning users have no classes @ all, due having count(sc.id) = 0
. linq
statement provided not filtered, ordered number of classes, should able extend desired filtering.
Comments
Post a Comment