I have the following SQL query:
SELECT
s.name, count(sc.id) AS classes
FROM
students s
LEFT JOIN studentsclasses sc ON s.id = sc.studentid
GROUP BY s.name
HAVING count(sc.id) = 0
ORDER BY count(sc.id);
That query gets the count of students classes and returns the students with least classes.
How can I convert this into C#? My attempt does not yield the expected result. I have:
var query = (from students in ent.Students
join classes in ent.StudentsClasses on students.ID equals classes.StudentID into gj
from subpet in gj.DefaultIfEmpty()
select new { students.Name }).ToList();
However that returns all the names of all the students registered in courses.
================================
Here are the tables:
==================
StudentsClasses
----------------
ID (Registration ID of the class)
StudentID (ID of student taking class)
ClassID (ID of certain 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 into gj
from subpet in gj.DefaultIfEmpty()
orderby students.StudentsClasses.Count
where students.StudentsClasses.Count == 0
select new { students.Name }).ToList();
Student.Classes.Count()for each student - more succinctly, can you post the model classes for Student and StudentsClasses?orderby students.StudentsClasses.Countyielded the correct output. Thanks @jdphenix