[英]Use inner join in linq
您能幫我在EmployeController(MVC 4)中加入表格(Сотрудник和Должность),
源代碼 :
public ViewResult List(int page = 1)
{
EmployeListViewModel viewModel = new EmployeListViewModel
{
Employes = repository.Сотрудник
.OrderBy(e => e.FAM).ThenBy(n => n.Name).Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
itemsPerPage = PageSize,
TotalItems = repository.Сотрудник.Count()
}
};
return View(viewModel);
}
員工的源存儲庫:
using System.Linq;
using WebService.Domain.Abstract;
using WebService.Domain.Entities;
namespace WebService.Concrete
{
public class EFEmployeRepository: IEmployeRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Сотрудник> Сотрудник
{
get { return context.Сотрудник; }
}
}
}
我需要連接表的幫助(Сотрудник[appointmnet_id]和Должность[ID])
在Сотрудник上創建一個名為Должность類型的約會的屬性。 然后,您無需加入,只需要遵循存儲庫中的navigation屬性。
編輯:一個代碼示例
類定義:
public class Сотрудник {
...
public int appointmnet_id { get; set; }
[ForeignKey(appointment_id)]
public Должность appointment { get; set; }
}
儲存庫方法:
public List<Сотрудник> GetStuff(int page, int PageSize) {
return (
from e in repository.Сотрудник.Include("appointment")
orderby e.FAM
thenby e.Name
select e
).Skip((page - 1) * PageSize).Take(PageSize).ToList();
}
MVC動作:
public ViewResult List(int page = 1)
{
EmployeListViewModel viewModel = new EmployeListViewModel
{
Employes = GetStuff(page, PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
itemsPerPage = PageSize,
TotalItems = repository.Сотрудник.Count()
}
};
return View(viewModel);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.