我刚刚阅读了Adam Freeman的Pro ASP.NET MVC一书,作为ASP.NET MVC的介绍。
然而,我遇到了一种情况,我必须加入多个表,但是使用Freeman的方法,我无法做到。
请参阅下面的代码
EFEmployeeRepository.cs(在我的具体文件夹中)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HRMS.Domain.Abstract;
using HRMS.Domain.Entities;
namespace HRMS.Domain.Concrete
{
public class EFEmployeeRepository : IEmployeesRepository
{
private EFDbContext context = new EFDbContext();
public Employee getEmployeeByID(int User_ID)
{
// I want to Join the Employee Table with the User Table to Get Employee Details
Employee employee = context.Employees.FirstOrDefault(p => p.User_ID == User_ID);
return employee;
}
}
}
IEmployeesRepository.cs(Absctract文件夹)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HRMS.Domain.Entities;
namespace HRMS.Domain.Abstract
{
public interface IEmployeesRepository
{
//Get employee Details
Employee getEmployeeByID (int User_ID);
}
}
我需要将Employee模型与特定Employee的用户模型一起加入并将数据返回给控制器
答案 0 :(得分:5)
var empDetails = (from emp in Context.Employees
join us in Context.User on emp.User_ID equals us.User_ID
where emp.User_ID == User_ID
select new UserDataModel
{
User_ID=us.User_ID,
UserName=us.username,
EmployeeId=emp.EmployeeId
}).FirstOrDefault()
您的数据模型:
public class UserDataModel
{
public int User_ID {get; set;}
public String UserName {get; set;}
public int EmployeeId {get; set;}
}