如何使用linq将多个模型组合为一个模型

时间:2020-02-10 18:00:55

标签: c# linq asp.net-core

我有一个SecurityLog模型,它从sql数据库返回行。我还有一个SecurityLogOfficer和Officer表,其中包含许多关系。

如何将模型合并在一起并显示人员名称的串联列表?

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列表,并在一行上显示人员名称列表(以逗号分隔)

enter image description here

更新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);

2 个答案:

答案 0 :(得分:2)

这是您可以参考的有效演示:

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

4。结果: enter image description here

答案 1 :(得分:1)

在将数据库实体映射到ViewModel时,应使用display属性进行串联或串联(如示例中的注释行)。鉴于它可能仅用于显示目的,因此您不应该在db级imo上使用该逻辑。

相关问题