将 SQL 查询转换为 linq C#
Convert SQL query into linq C#
将 SQL 查询转换为 LINQ C#
SELECT ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId = 2352
这是我的 LINQ 查询
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);
var teacherId = ; // your teacherId here
var result = new {
TeacherId = teacherId,
Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
或者您可以按 SubjectId
:
分组
var teacherId = ; // put your teacherId here
var result =
(from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
group ts by ts.SubjectId into gr
select new
{
TeacherId = teacherId,
Subjects = gr.Count()
}).ToList();
将 SQL 查询转换为 LINQ C#
SELECT ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId = 2352
这是我的 LINQ 查询
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);
var teacherId = ; // your teacherId here
var result = new {
TeacherId = teacherId,
Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
或者您可以按 SubjectId
:
var teacherId = ; // put your teacherId here
var result =
(from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
group ts by ts.SubjectId into gr
select new
{
TeacherId = teacherId,
Subjects = gr.Count()
}).ToList();