[英]How to combine multiple models into one using linq
我有一個從 sql 數據庫返回行的 SecurityLog 模型。 我還有一個包含多對多關系的 SecurityLogOfficer 和官員表。
如何將模型合並在一起並顯示軍官姓名的串聯列表?
IQueryable<SecurityLog> sort = from s in _context.SecurityLog
.Include(f => f.SecurityLogOfficers)
.ThenInclude(e => e.Officer)
//.Select(x => new PageViewModel
//{ OfficersNames = string.Join(",", x.SecurityLogOfficers.Select(o => o.Officer.FullName)) })
select s;
基本上我想把我的 SecurityLogOfficers 列表和顯示在一行中的官員姓名列表(逗號分隔)
更新 02/11/20
@foreach (var item in Model.SecurityLog)
{
<tr>
<td style="width:4% !important">
@Html.DisplayFor(modelItem => item.ID)
</td>
<td style="width:5% !important">
@Html.DisplayFor(modelItem => item.EventDate)
</td>
<td style="width:5% !important">
@Model.OfficerNames[i]
}
索引頁模型
public class PageViewModel
{
public List<string> OfficerNames { get; internal set; }
}
這給出了顯式轉換錯誤....
IQueryable<SecurityLog> sort = from s in _context.SecurityLog
.Include(a => a.Entity)
.Include(b => b.EventType)
.Include(c => c.Location)
.Include(d => d.ShiftRange)
.Include(f => f.SecurityLogOfficers)
.ThenInclude(e => e.Officer)
.Select(x => new PageViewModel
{ OfficerNames = string.Join(",", x.SecurityLogOfficers.Select(o => o.Officer.FullName)) })
select s;
if (!String.IsNullOrEmpty(searchString))
{
sort = sort.Where(s => s.Narrative.Contains(searchString)
|| s.SubjectFirst.Contains(searchString)
|| s.SubjectLast.Contains(searchString)
|| s.OfficerNames.Contains(searchString));
}
SecurityLog = await PaginatedList<SecurityLog>.CreateAsync(sort
.Include(a => a.Entity)
.Include(b => b.EventType)
.Include(c => c.Location)
.Include(d => d.ShiftRange)
.Include(e => e.Officer)
.Include(f => f.SecurityLogOfficers)
.AsNoTracking(), pageIndex ?? 1, pageSize);
這是您可以參考的工作演示:
1.型號:
public class SecurityLog
{
public int SecurityLogId { get; set; }
public string Name { get; set; }
public IList<SecurityLogOfficers> SecurityLogOfficers { get; set; }
}
public class Officer
{
public int OfficerId { get; set; }
public string Active { get; set; }
public string FullName { get; set; }
public IList<SecurityLogOfficers> SecurityLogOfficers { get; set; }
}
public class SecurityLogOfficers
{
public int OfficerId { get; set; }
public Officer Officer { get; set; }
public int SecurityLogId { get; set; }
public SecurityLog SecurityLog { get; set; }
}
2.Index.cshtml:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.SecurityLog[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.SecurityLog[0].SecurityLogOfficers[0].Officer.FullName)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.SecurityLog) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@(string.Join(",", item.SecurityLogOfficers.Select(u=>u.Officer.FullName)))
</td>
</tr>
}
</tbody>
</table>
3.Index.cshtml.cs:
public IList<SecurityLog> SecurityLog { get;set; }
public async Task OnGetAsync()
{
IQueryable<SecurityLog> sort = from s in _context.SecurityLog
.Include(f => f.SecurityLogOfficers)
.ThenInclude(e => e.Officer)
select s;
SecurityLog = await sort.ToListAsync();
}
您應該使用顯示屬性,該屬性將在將 db 實體映射到 ViewModel 時進行連接或進行連接(如示例中的注釋行)。 鑒於它可能僅用於顯示目的,您不應該在數據庫級別 imo 中引入該邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.