我的查詢返回匹配特定位置的人員列表。我希望它能夠返回所有職位的列表,即使沒有人匹配職位。我加入人們的原因是因爲我需要「Assigned」的計數,這是一個與位置相匹配的動態添加計數字段。但是,如果有匹配誰,我希望它仍然返回駐紮位置的位置,但不想要的人「分配」設置爲0。LINQ返回MVC5上的所有記錄和groupby Razor查看
dynamic query = (from a in db.Positions
join b in db.People
on new {
a.GradeId,
a.SeriesId,
a.CompanyId,
a.PaybandId }
equals new {
b.GradeId,
b.SeriesId,
b.CompanyId,
b.PaybandId } into ab
from k in ab.DefaultIfEmpty()
join c in db.Grades on k.GradeId equals c.Id
join d in db.Series on k.SeriesId equals d.Id
join e in db.Companies on k.CompanyId equals e.Id
join p in db.Paybands on k.PaybandId equals p.Id
group a by new { CompanyName = e.Name,
GradeName = c.Name,
SeriesName = d.Name,
PaybandName = p.Name,
a.Authorized } into f
select new { Company = f.Key.CompanyName,
Grade = f.Key.GradeName,
Series = f.Key.SeriesName,
Payband = f.Key.PaybandName,
Authorized = f.Key.Authorized,
Assigned = f.Count() }).AsEnumerable().Select(r => r.ToExpando());
我面臨的另一個問題是,剃刀頁面上,該公司正在重複。我需要公司每個公司僅出現一次,但希望所有職位都列在其下。
@model IEnumerable<dynamic>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
<table class="table table-responsive table-hover">
<thead>
<tr>
<th class="col-sm-1"></th>
<th class="col-sm-2">Grade</th>
<th class="col-sm-2">Series</th>
<th class="col-sm-2">Payband</th>
<th class="col-sm-2">Authorized</th>
<th class="col-sm-2">Assigned</th>
</tr>
</thead>
@foreach (dynamic item in Model)
{
<thead>
<tr>
<th class="panel-bg" colspan="6">@item.Company</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-sm-1"></td>
<td class="col-sm-2">@item.Grade</td>
<td class="col-sm-2">@item.Series</td>
<td class="col-sm-2">@item.Payband</td>
<td class="col-sm-2">@item.Authorized</td>
<td class="col-sm-2">@item.Assigned</td>
</tr>
</tbody>
}
</table>
任何幫助,不勝感激!
EDIT(在請求添加Positions.cs類)
namespace CPR.Models
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Positions
{
public Positions()
{
}
[Key]
public int Id { get; set; }
[Required]
public int CompanyId { get; set; }
[Required]
public int SeriesId { get; set; }
[Required]
public int GradeId { get; set; }
[Required]
public int PaybandId { get; set; }
[Required]
public int Authorized { get; set; }
public virtual Companies Companies { get; set; }
public virtual Series Series { get; set; }
public virtual Grades Grades { get; set; }
public virtual Paybands Paybands { get; set; }
}
}
你可以添加你的職位類嗎? –
@MichaelBurns - 根據您的請求添加 – Element808