如何列出多个表中的数据但作为1条记录?
我有3张桌子
表1:服务
Id:01,名称:Service 1
表2:ServiceRequests
Id:01,ServiceId:01,请求者:John
表3:ServiceApprovers
Id:01,ServiceId:01,批准者:John ID:02,ServiceId:01,批准者:Linda
我的linq代码如下:
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="3"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red"></Grid>
</Grid>
<Grid Grid.Row="1">
<controls:GridSplitter GripperCursor="Help" HorizontalAlignment="Stretch" Grid.Column="1" ResizeDirection="Columns"
ResizeBehavior="CurrentAndNext" CursorBehavior="ChangeOnSplitterHover" VerticalAlignment="Stretch" ></controls:GridSplitter>
</Grid>
<Grid Grid.Row="2">
<TextBlock Text="aoisdjaisodjaiosdjsa" Width="60" Height="60" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Grid>
</Grid>
由于ServiceApprovers表,此代码将返回2条记录。 我该如何返回1条记录并连接批准人以成为“ John,Linda”?
我的控制器如下:
public IEnumerable<ServiceRequestsViewModel> ServiceRequestGetAll()
{
var result = (from srv in DB.Services
join srq in DB.ServiceRequests on srv.Id equals srq.ServiceId
join srp in DB.ServiceApprovers on srq.ServiceId equals srp.ServiceId
select new ServiceRequestsViewModel
{
Id = srq.Id,
ServiceId = srq.ServiceId,
RequestorId = srq.RequestorId,
ApproverId = srp.UserId,
Name = srv.Name,
Description = srq.Description,
Status = srq.Status,
Attachment = srq.Attachment,
CreatedBy = srq.CreatedBy,
CreatedDate = srq.CreatedDate
})
.OrderByDescending(z => z.CreatedDate);
return result;
}
我的查看页面如下:
public ActionResult Service_Request()
{
var dao = new ServicesDAO();
ViewBag.CurrentLogin = CurrentLoginDetail.UserName;
return PartialView("Service_Request", dao.ServiceRequestGetAll().ToList());
}
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: '/Services/ServiceRequestCount/',
success: function (response) {
$(".sidenotification").text(response);
}
})
$(document).on("click", '.view_button', function (e) {
var serviceid = $(this).data('id');
var name = $(this).data('name');
var desc = $(this).data('description');
var attchmnt = $(this).data('attachment');
var requestor = $(this).data('requestorid');
//var approver = $(this).data('approverid');
var createdby = $(this).data('createdby');
var createddate = $(this).data('createddate');
//alert(requestor);
$(".editService_ServiceId").val(serviceid);
$(".editService_Name").text(name);
$(".editService_Description").val(desc);
$(".editService_Attachment").val(attchmnt);
$(".editService_Requestor").val(requestor);
//$(".editService_Approver").val(approver);
$(".editService_CreatedBy").val(createdby);
$(".editService_CreatedDate").val(createddate);
});
$('[data-toggle="tooltip"]').tooltip();
});
<section class="section">
<div class="row">
<div class="col-md-12">
<h2>Service Request Listing</h2>
<br />
<div class="row">
<div class="container">
<table class="table">
<thead>
<tr>
@*<th>Id</th>
<th>Service Id</th>*@
<th>Name</th>
<th>Requestor</th>
<th>Description</th>
<th>Submitted Date</th>
<th>Status</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model.Where(i => i.RequestorId == ViewBag.CurrentLogin || i.ApproverId == ViewBag.CurrentLogin))
{
<tbody>
<tr>
<td>@item.Name</td>
<td>@item.RequestorId</td>
<td>@item.Description</td>
<td>@item.CreatedDate.ToString("dd-MMM-yyyy")</td>
@if (item.Status == "Submitted")
{
@*<td><div class="btn btn-static" style="cursor:default;">@item.Status</div></td>*@
<td><div class="btn btn-staticsubmit badge-pill badge-primary" style="cursor:default;" data-toggle="tooltip" title="Request submitted and pending for approval">@item.Status</div></td>
}
@if (item.Status == "Approved")
{
@*<td><div class="btn btn-static" style="cursor:default;">@item.Status</div></td>*@
<td><div class="btn btn-staticapprove badge-pill badge-primary" style="cursor:default;" data-toggle="tooltip" title="Request has been approved">@item.Status</div></td>
}
@if (item.Status == "Rejected")
{
@*<td><div class="btn btn-static" style="cursor:default;">@item.Status</div></td>*@
<td><div class="btn btn-staticreject badge-pill badge-primary" style="cursor:default;" data-toggle="tooltip" title="Request has been rejected">@item.Status</div></td>
}
<td>
<button type="button" class="btn view_button" href="#viewServiceModal" data-toggle="modal"
data-id="@item.Id"
data-name="@item.Name"
data-description="@item.Description"
data-attachment="@item.Attachment"
data-requestorid="@item.RequestorId"
data-createdby="@item.CreatedBy"
data-createddate="@item.CreatedDate">
View
</button>
</td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
视图页面上不需要我的列表来显示“批准者”,但是我需要包括“批准者”,以便我可以将它们传递给我的模态。
答案 0 :(得分:0)
只需按ServiceId将结果分组
result.GroupBy(_ => _.ServiceId)
并更改模型的UserId
,以使其可以容纳多个用户:
IEnumerable<string> UsersId { get; set; }