简体   繁体   English

在linq中使用内部联接

[英]Use inner join in linq

Can you help me with joining tables(Сотрудник and Должность) in EmployeController (MVC 4), 您能帮我在EmployeController(MVC 4)中加入表格(Сотрудник和Должность),

Source code : 源代码 :

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);
}

Source repository for Employeess: 员工的源存储库:

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.Сотрудник; }
        }
    }
}

I need help for joining tables(Сотрудник[appointmnet_id] with Должность[ID]) 我需要连接表的帮助(Сотрудник[appointmnet_id]和Должность[ID])

Create a property on Сотрудник called Appointment of type Должность. 在Сотрудник上创建一个名为Должность类型的约会的属性。 Then you need not join, you need only follow the navigation property inside the repository. 然后,您无需加入,只需要遵循存储库中的navigation属性。

EDIT: A code sample 编辑:一个代码示例

Class definition: 类定义:

public class Сотрудник {
    ...

    public int appointmnet_id { get; set; }
    [ForeignKey(appointment_id)]
    public Должность appointment { get; set; }
}

Repository Method: 储存库方法:

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 Action: 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.

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