简体   繁体   English

在 asp.net 的列上查找具有相同值的行

[英]Find rows that have the same value on a column in asp.net

I have an issue I want to translate this SQL query int asp.net.我有一个问题,我想翻译这个 SQL 查询 int asp.net。 there is a relationship between student table and teacher table many to many and I want to display students that have more than one teacher学生表和教师表之间存在多对多关系,我想显示拥有多个教师的学生

select COUNT(StudentId), StudentId  from StudentTeachers 
group by StudentId
 having COUNT(StudentId)> 1

and that what I trying to do in asp.net以及我在 asp.net 中尝试做的事情

 var data = db.StudentTeachers.
                Join(db.Students, a => a.StudentId, b => b.ID, (a, b) =>a).GroupBy(x=> x.StudentId);

You can try out both ways.您可以尝试两种方式。

Using Lambda Expressions使用 Lambda 表达式

            db.StudentTeachers.GroupBy(s => s.StudentId)
                  .Where(grp => grp.Count() > 1)
                  .Select(grp => new
                  {
                      StudentCount = grp.Count(),
                      StudentId = grp.Key
                  }).ToList();

Using SQL like query expressions使用 SQL 之类的查询表达式

from s in db.StudentTeachers
group s by s.StudentId into grp
where grp.Count() > 1
select new { StudentId = grp.Key, StudentCount = grp.Count() }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM