所以这是我有这个查询的问题,它返回视图的数据
reportData = dbContext.FinancialsBySupplierAuditPeriodStatusType
.Where(v => v.ReviewPeriodID == reportFilter.ReviewPeriodID && v.StatusCategoryID == reportFilter.StatusCategoryID && v.ClientID == reportFilter.ClientId)
.GroupBy(s => new { s.SupplierID })
.Select(g => new DrilldownReportItem {
SupplierID = g.Key.SupplierID,
SupplierName = g.Max(v => v.SupplierName),
AccountNo = g.Max(v => v.AccountNo),
SuppTotals = g.Select(v => new TempTotals { ClaimType = v.TypeDesc ?? "Old Claims", Amount = v.Amount ?? 0 })
}).OrderBy(r => r.SupplierName).ToList();
SuppTotals包含一个IEnumerable,其中包含每个ClaimTypes,但是如果该记录没有值,它们可能不包含每个ClaimType,因此在视图上将有5个标题,但在表格行中只有两个结果。 / p>
例如,有5个ClaimTypes,但该记录只有其中一个,然后在视图上不允许标题匹配,数据显示为空白。
我用这个
修复了这个问题 var existing = i.SuppTotals.Select(x => x.ClaimType);
var toAdd = Model.ClaimHeadings.Except(existing).Select(x => new TempTotals {
ClaimType = x,
Amount = 0
});
var row = i.SuppTotals.ToList();
row.AddRange(toAdd);
@foreach (var item in Model.ClaimHeadings) {
<td><span class="text">@String.Format("{0:C0}", row.Where(c => c.ClaimType == item).First().Amount)</span></td>
所以我的问题是,是否有办法将此添加范围合并到查询本身中,这样我就不必将此代码放在视图中并将其放入查询中。
- 更新 -
这是声明类型实体
public partial class ClaimType
{
[Key][Column(Order = 0)]
public int ClientID { get; set; }
[Key][Column(Order = 1)]
[StringLength(2)]
//WAS STRING
public string TypeID { get; set; }
[StringLength(50)]
public string TypeDesc { get; set; }
[StringLength(2)]
public char OldTypeID { get; set; }
}
此代码将所有声明类型拉为0,以添加到报表数据中的
var claimTypes = _claimManager.GetClaimTypes(PredicateBuilder.True<ClaimType>().And(x => x.ClientID == reportFilter.ClientId)).Select(x => new TempTotals {
ClaimType = x.TypeDesc,
Amount = 0
});
答案 0 :(得分:1)
您可以尝试使用 antijoin 和Concat
向每个组添加缺少的声明类型:
SuppTotals = g.Select(v => new TempTotals { ClaimType = v.TypeDesc ?? "Old Claims", Amount = v.Amount ?? 0 })
.Concat(from ct in dbContext.ClaimType
join v in g on ct.TypeDesc equals v.TypeDesc into match
from v in match.DefaultIfEmpty()
where v == null
select new TempTotals { ClaimType = ct.TypeDesc, Amount = 0 })