如何使用Linq连接两个表并计算一列匹配记录的行数

如何使用Linq连接两个表并计算一列匹配记录的行数,linq,join,Linq,Join,您好,我有两个表格:教师和部门 我的Teacher表中有这些列(Teacherid,Name,Department) 我的部门表有(id,Department\u name)列 现在,我想显示部门列表,我还想显示拥有该部门的教师总数,以及如何使用linq查询来实现这一点。因此,在教师和部门之间有一个简单的一对多关系:每个部门都有零名或多名教师,每个教师都在一个部门工作,即外键部门ID所指的部门 当你有一对多关系时,你希望所有的“客户有他们的订单”,“有他们的书的作者”,有他们的老师的部门“,考虑

您好,我有两个表格:
教师
部门

我的
Teacher
表中有这些列(
Teacherid
Name
Department

我的
部门
表有(
id
Department\u name
)列


现在,我想显示部门列表,我还想显示拥有该部门的教师总数,以及如何使用linq查询来实现这一点。

因此,在
教师
部门
之间有一个简单的一对多关系:每个
部门
都有零名或多名
教师
,每个
教师
都在一个
部门
工作,即外键
部门ID
所指的部门

当你有一对多关系时,你希望所有的“客户有他们的订单”,“有他们的书的作者”,有他们的老师的部门“,考虑使用其中的一个.

如果您想要的不仅仅是“带教师的部门”,请使用带有参数
resultSelector

IQueryable<Teacher> teachers = ...
IQueryable<Department> departments = ...

// GroupJoin: get the Departments with their Teachers
var result = departments.GroupJoin(teachers,

    department => department.Id,     // from every department take the primary key
    teacher => teacher.DepartmentId, // from every teacher take the foreign key

    // parameter resultSelector: from every Department, with its zero or more Teachers,
    // make one new:
    (department, teachersOfThisDepartment) => new
    {
        // Select only the department properties that you plan to use:
        Id = department.Id,
        Name = department.Name,
        ...

        // Get the total number of teachers that work on this department:
        TeacherCount = teachersOfThisDepartment.Count(),

        // If you want more information about the Teachers, use Select:
        Teachers = teachersOfThisDepartment.Select(teacher => new
        {
            // again: select only the teacher properties that you plan to use:
            Id = teacher.Id,
            Name = teacher.Name,
            ...

            // not needed, you already know the value
            // DepartmentId = teacher.DepartmentId,
        })
        .ToList(),
    });
IQueryable教师=。。。
IQueryable部门=。。。
//GroupJoin:让各系的老师一起参加
var result=departments.GroupJoin(教师、,
department=>department.Id,//从每个部门获取主键
teacher=>teacher.DepartmentId,//从每个教师获取外键
//参数结果选择器:来自每个部门,其零名或多名教师,
//制作一个新的:
(系,教师本系)=>新建
{
//仅选择您计划使用的部门属性:
Id=部门Id,
Name=部门名称,
...
//获取在该部门工作的教师总数:
TeacherCount=teachersOfThisDepartment.Count(),
//如果需要有关教师的更多信息,请使用选择:
教师=本系教师。选择(教师=>新建
{
//再次:仅选择您计划使用的教师属性:
Id=teacher.Id,
Name=老师。Name,
...
//不需要,您已经知道其价值
//DepartmentId=teacher.DepartmentId,
})
.ToList(),
});
在一对多关系中,如果希望“项及其零个或多个子项”使用GroupJoin,如果希望“子项及其一个且唯一的父项”使用Join


假设您的模型如下所示:

公共课堂教师
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int DepartmentId{get;set;}
}
公共课系
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
让我们创建一些示例数据:

var部门=新列表
{
新部门{Id=1,Name=“A”},
新部门{Id=2,Name=“B”},
新部门{Id=3,Name=“C”}
};
var教师=新名单
{
新教师{Id=1,Name=“AA”,DepartmentId=1},
新教师{Id=2,Name=“AB”,部门Id=1},
新教师{Id=3,Name=“CA”,DepartmentId=3}
};
让我们加入它们并计算请求的输出:

var报告=来自部门中的部门
加入部门教师中的教师。Id等于教师。部门Id加入教师部门
选择新的
{
部门名称=部门名称,
NumberOfTeachers=department.Count()中的教师
};
最后打印出报告:

foreach(报告中的var记录)
{
Console.WriteLine($“{record.DepartmentName}:{record.NumberOfTeachers}”);
}

我正在使用上述方法,希望查看报表变量中的内容,但它显示“Error=函数求值需要所有线程才能运行。”@Tabor我想您在调试过程中遇到了这种情况。您可以修复它或在查询结束时显式调用
ToList()
,如下所示:
var report=(from….Count())托利斯先生()
感谢您的帮助,您的Linq查询工作正常,但请告诉我如何将这些值用于我的视图,因为在我的视图中,我通过迭代到模型对象来调用模型值。Status=department.Status,CreationDate=department.Date\u Created,DepartmentName=department.department\u Title,NumberOfTeachers=teachersInDepartment.Count()如何在我的视图中使用这四个变量。@Tabor这里我创建了一个匿名类型(
select new{…}
),只包括系名和相关教师的人数(如原始问题所述)。如果需要返回特定的内容,则需要在select子句
selectnewyourreportmodel{Status..}
| Id | Department_Name |
|----|-----------------|
|    |                 |
IQueryable<Teacher> teachers = ...
IQueryable<Department> departments = ...

// GroupJoin: get the Departments with their Teachers
var result = departments.GroupJoin(teachers,

    department => department.Id,     // from every department take the primary key
    teacher => teacher.DepartmentId, // from every teacher take the foreign key

    // parameter resultSelector: from every Department, with its zero or more Teachers,
    // make one new:
    (department, teachersOfThisDepartment) => new
    {
        // Select only the department properties that you plan to use:
        Id = department.Id,
        Name = department.Name,
        ...

        // Get the total number of teachers that work on this department:
        TeacherCount = teachersOfThisDepartment.Count(),

        // If you want more information about the Teachers, use Select:
        Teachers = teachersOfThisDepartment.Select(teacher => new
        {
            // again: select only the teacher properties that you plan to use:
            Id = teacher.Id,
            Name = teacher.Name,
            ...

            // not needed, you already know the value
            // DepartmentId = teacher.DepartmentId,
        })
        .ToList(),
    });
A: 2
B: 0
C: 1