简体   繁体   English

MVC-如何使用 Linq ASP.NET MVC 连接两个表或模型?

[英]MVC-How to Join Two Tables or Models using Linq ASP.NET MVC?

I am working with asp.NET MVC .我正在使用asp.NET MVC And I am trying to find Salary Grades of employees [as we find in oracle 12c using scott schema].我正在尝试找到员工的工资等级 [正如我们在 oracle 12c 中使用 scott 模式找到的那样]。 I have created 4 Models EMP, DEPT, Bonus, and SalGrade (Same as scott schema in oracle 12c) .我创建了 4 个模型EMP、DEPT、Bonus 和 SalGrade(与 oracle 12c 中的 scott 模式相同) The query for finding grades in Oracle-12c is following.在 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;

I just need to convert the above given query into ASP.NET MVC LINQ query.我只需要将上面给定的查询转换为 ASP.NET MVC LINQ 查询。

Models I have created are follwing.我创建的模型如下。

Dept Models:部门型号:

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

EMP Model:电磁脉冲模型:

 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 Model SalGrade 模型

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

In your controller class method, you might have some code like this:在您的控制器类方法中,您可能有一些这样的代码:

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...
}   

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

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