如何使用LINQ从多个表中显示一行的详细信息?

时间:2019-05-22 10:43:47

标签: asp.net-mvc

在这种情况下,我应该显示一个人的详细信息以及他/她的分配清单。我已经完成了创建视图模型以将数据传递到视图的操作,但是结果是:

  

传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery`1 [EKCMIDTA.ViewModels.EmployeeDetailsVM]',但是此字典需要类型为'EKCMIDTA.ViewModels.EmployeeDetailsVM'的模型项

对于 TicketScannings 表,我只想知道此人是否使用了一些分配,并计算使用了多少分配,无论它是否为空。

我希望有人可以帮助我。

谢谢!

控制器:

public ActionResult GetDetails(int empId)
{
    var employeeInformation = identityContext.AspNetUsers.Find(empId);

    var employeeDetails = dbContext.TicketAllocations.Include(a => a.AllocationCategory).Where(t => t.CMId == empId).ToList();

    var query = (from alloc in dbContext.TicketAllocations
                 join scan in dbContext.TicketScannings
                 on alloc.Id equals scan.TicketAllocationId
                 join card in dbContext.CardNumberAssignments
                 on alloc.CMId equals card.CMId into a
                 from card in a.DefaultIfEmpty()
                 join reserve in dbContext.ReservedCardNumbers
                 on card.CardNumberId equals reserve.Id into b
                 from reserve in b.DefaultIfEmpty()
                 where (alloc.CMId == empId)
                 select new EmployeeDetailsVM()
                 {
                     Employee = new Employee()
                     {
                         FirstName = employeeInformation.FirstName,
                         LastName = employeeInformation.LastName,
                         CMId = employeeInformation.Id,
                         CardNumber = reserve == null ? "No Card Number yet" : reserve.CardNumber,
                         QRCode = card == null ? "No QR Code yet" : card.QRCode
                     },

                     GetTicketAllocations = employeeDetails
                 });

      return View(query);

查看模型:

public class EmployeeDetailsVM
{
    public Employee Employee { get; set; }
    public IEnumerable<Allocation> GetTicketAllocations { get; set; }
}

public class Employee
{
    public string CMId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string CardNumber { get; set; }
    public string QRCode { get; set; }
}

public class Allocation
{
    public int AllocationId { get; set; }
    public string AllocationName { get; set; }
    public int Quantity { get; set; }
    public bool IsActive { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
}

查看:

@model EKCMIDTA.ViewModels.EmployeeDetailsVM

1 个答案:

答案 0 :(得分:0)

看起来您的视图仅接受单个EmployeeDetailsVM的模型,但是您传递的查询可能返回多个。

因此您可以将@model EKCMIDTA.ViewModels.EmployeeDetailsVM更改为@model IEnumerable<EKCMIDTA.ViewModels.EmployeeDetailsVM>

或将您的GetDetails操作更改为return View(query.FirstOrDefault());

基于评论进行编辑

public ActionResult GetDetails(int empId)
{
    var employeeInformation = identityContext.AspNetUsers.Find(empId);

    var employeeTickets = dbContext.TicketAllocations.Include(a => a.AllocationCategory).Where(t => t.CMId == empId).ToList();

    var employeeDetails = (from alloc in dbContext.TicketAllocations
                 join scan in dbContext.TicketScannings
                 on alloc.Id equals scan.TicketAllocationId
                 join card in dbContext.CardNumberAssignments
                 on alloc.CMId equals card.CMId into a
                 from card in a.DefaultIfEmpty()
                 join reserve in dbContext.ReservedCardNumbers
                 on card.CardNumberId equals reserve.Id into b
                 from reserve in b.DefaultIfEmpty()
                 where (alloc.CMId == empId)
                 select new EmployeeDetailsVM()
                 {
                     Employee = new Employee()
                     {
                         FirstName = employeeInformation.FirstName,
                         LastName = employeeInformation.LastName,
                         CMId = employeeInformation.Id,
                         CardNumber = reserve == null ? "No Card Number yet" : reserve.CardNumber,
                         QRCode = card == null ? "No QR Code yet" : card.QRCode
                     }
                 }).FirstOrDefault();

    if (employeeDetails != null)
        employeeDetails.GetTicketAllocations = employeeTickets;

    return View(employeeDetails);
}