【问题标题】:How to write a subquery in a select clause in linq如何在 linq 的 select 子句中编写子查询
【发布时间】:2022-01-13 01:44:21
【问题描述】:

我正在尝试将以下 sql 查询最好地转换为 linq 查询(查询语法)。

SQL 查询:

SELECT e.Id, e.Name, e.Salary, e.DepartmentId, (select d.dept_name from department d where d.dept_id = e.DepartmentId) as dpt
FROM [Employee] e

【问题讨论】:

    标签: sql linq


    【解决方案1】:

    创建一个组合数据列表的模型(即员工和部门)并定义您需要检索的 poco 类。

    var combinedDataList =
        from a in _context.employeeTable
        from b in _Context.department 
        where a.DepartmentId=b.dept_id
        Select new Combinetable (
            Id = a .Id,
            Name = a .Name,
            Salary = a .Salary,
            DeptId = a .DepartmentId
            DepartmentName = b.dept_name 
        ).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2014-02-23
      相关资源
      最近更新 更多