【问题标题】:MVC-How to Join Two Tables or Models using Linq ASP.NET MVC?MVC-如何使用 Linq ASP.NET MVC 连接两个表或模型?
【发布时间】:2020-03-20 11:33:32
【问题描述】:

我正在使用 asp.NET MVC。我正在尝试查找员工的薪资等级[正如我们在 oracle 12c 中使用 scott 模式找到的那样]。我创建了 4 个模型 EMP、DEPT、Bonus 和 SalGrade(与 oracle 12c 中的 scott 架构相同)。在 Oracle-12c 中查找成绩的查询如下。

SELECT s.grade, count(*), max(sal)
 FROM EMP e, SalGrade s
   WHERE e.sal BETWEEN s.LoSal AND s.HiSal
     GROUP BY s.grade;

我只需要将上面给出的查询转换为 ASP.NET MVC LINQ 查询。

我创建的模型如下。

部门模型:

public class Department{
[Key]
public int Deptno { get; set; }
public string Dname { get; set; }
public string  Loc { get; set; }}

EMP 模型:

 public class Employee
{
    [Key]
    public int Empno { get; set; }
    public string  Ename { get; set; }
    public string Job { get; set; }
    public int Mgr { get; set; }
    public DateTime Hiredate { get; set; }
    public int Sal { get; set; }
    public int Comm { get; set; }
    public int Deptno { get; set; }
    public Department Department { get; set; }

}

SalGrade 模型

public class Salgrade
{
    [Key]
    public int Grade { get; set; }
    public int LoSal { get; set; }
    public int HiSal { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc entity-framework linq


    【解决方案1】:

    在你的控制器类方法中,你可能有一些这样的代码:

    using(var db = new DepartmentEntities()){
    
        var query = from db.Employee, db.Salgrade
                    where db.Employee.Sal between db.Salgrade.LoSal and db.Salgrade.HiSal
                    group by db.Salgrade.Grade
                    select new { db.Salgrade.Grade, count(*), max(db.Employee.Sal) };
    
        // Use query results in a foreach loop or whatever...
    }   
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多