简体   繁体   中英

Use inner join in linq

Can you help me with joining tables(Сотрудник and Должность) in 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])

Create a property on Сотрудник called Appointment of type Должность. Then you need not join, you need only follow the navigation property inside the repository.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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