C# MVC如何使用Linq ASP.NET MVC连接两个表或模型?
我正在使用asp.NET MVC。我正试图找到员工的工资等级[正如我们在使用scott模式的oracle 12c中所发现的那样]。我已经创建了4个模型EMP、DEPT、Bonus和SalGrade(与oracle 12c中的scott模式相同)。在Oracle-12c中查找成绩的查询如下C# MVC如何使用Linq ASP.NET MVC连接两个表或模型?,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,我正在使用asp.NET MVC。我正试图找到员工的工资等级[正如我们在使用scott模式的oracle 12c中所发现的那样]。我已经创建了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
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; }}
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; }
}
EMP型号:
public class Department{
[Key]
public int Deptno { get; set; }
public string Dname { get; set; }
public string Loc { get; set; }}
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; }
}
在控制器类方法中,您可能有如下代码:
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...
}