列出来自2个不同表/模型的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列出来自2个不同表/模型的数据相关的知识,希望对你有一定的参考价值。
我试图在视图页面中显示列表,从2个不同的表/模型中获取数据。
(表1)Table_Service:
- ID
- name
(表2)Table_Request:
- SERVICE_ID
- 描述
- 请求者
- 状态
表1和表2是Id / Service_Id的链接。
我的视图页面应该是(表2)Table_Request列表:
- SERVICE_ID
- 名称(来自表1)
- 描述
- 请求者
- 状态
我的观看页面如下:
@model IEnumerable<OnlinePlatform.Models.ServiceRequests>
<section class="section">
<div class="row">
<div class="col-md-12">
<h2>Please choose a service to start</h2>
<br />
<div class="row">
<div class="container">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Start Date</th>
<th>System Owner</th>
<th>Created By</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model)
{
<tbody>
<tr>
<td>@item.Id</td>
<td>@item.Id</td>
<td>@item.ServiceId</td>
<td>@item.Name</td>
<td>@item.RequestorId</td>
<td>@item.Description</td>
<td>@item.Status</td>
<td><button type="button" class="btn">Edit</button></td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
我的控制器如下:
public ActionResult Service_Request()
{
var dao = new ServicesDAO();
return PartialView("Service_Request", dao.ServiceRequestGetAll().ToList());
}
public IQueryable<ServiceRequests> ServiceRequestGetAll()
{
var result = DB.ServiceRequests.OrderBy(r => r.Id);
return result;
}
如何显示从(Table1)Table_Service获取的名称?
答案
由于您希望将混合结果集作为IQueryable<T>
返回,因此应使用Join()
查询并将查询结果放入viewmodel中:
1)Lambda版本
public IEnumerable<ServiceRequestsVM> ServiceRequestGetAll()
{
var result = DB.ServiceRequests.Join(DB.Services,
x => x.Service_Id, // Source table foreign key
y => y.Id, // Joined table primary key
(x, y) => new ServiceRequestsVM { Id = x.Id, Service_Id = x.Service_Id, Name = y.Name, Description = x.Description, Requestor = x.Requestor, Status = x.Status })
.OrderBy(z => z.Service_Id).ToList();
return result;
}
2)查询表达式版本
public IEnumerable<ServiceRequestsVM> ServiceRequestGetAll()
{
var result = (from srv in DB.Services
join srq in DB.ServiceRequests
on srv.Id equals srq.Service_Id
select new ServiceRequestsVM
{
Id = srq.Id,
Service_Id = srq.Service_Id,
Name = srv.Name,
Description = srq.Description,
Requestor = srq.Requestor,
Status = srq.Status
}).OrderBy(z => z.Service_Id).ToList();
return result;
}
Viewmodel示例
public class ServiceRequestsVM
{
public int Id { get; set; }
public int Service_Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Requestor { get; set; }
public string Status { get; set; }
}
然后更改@model
指令以使用具有两个表的组合属性的viewmodel:
@model IEnumerable<OnlinePlatform.Models.ServiceRequestsVM>
控制器应如下所示:
public ActionResult Service_Request()
{
var dao = new ServicesDAO();
return PartialView("Service_Request", dao.ServiceRequestGetAll());
}
完成这些步骤后,模型绑定应该完全起作用。
另一答案
通过使用ID连接两个表来获取记录。
仅供参考,
var result= from req in DB.Table_Request
join service in DB.Table_Service
on req.Service_id equals service.id
select new ServiceRequests
{
Name=service.Name,
ServiceID=req.Service_Id,
// Followed model Properties
}.ToList();
另一答案
我建议你创建这样的服务:
IServiceManager.cs:
public interface IServiceManager
{
IEnumerable<ServiceRequests> GetAllServiceRequests();
Service GetService(string serviceId);
}
ServiceManager.cs:
public class ServiceManager : IServiceManager
{
public ApplicationDbContext _dbContext { get; set; }
public ServiceRequest(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<ServiceRequests> GetAllServiceRequests()
=> _dbContext.ServiceRequests.OrderBy(r => r.Id);
public Service GetService(string serviceId)
=> !string.IsNullOrEmpty(serviceId)
? _dbContext.Services.SingleOrDefault(x => x.Id == serviceId) : null;
}
Startup.cs:
services.AddTransient<IServiceManager, ServiceManager>();
控制器:
public ActionResult Service_Request()
{
return View();
}
Service_Request.cshtml:
@model IEnumerable<OnlinePlatform.Models.ServiceRequests>
@inject IServiceManager serviceManager
@foreach (var request in serviceManager.GetAllServiceRequests())
{
var service = serviceManager.GetService(request.Service_Id);
<!-- you can use @service.Name here... -->
}
以上是关于列出来自2个不同表/模型的数据的主要内容,如果未能解决你的问题,请参考以下文章