我有一些有用的东西,但不知道如何将它们组合在一起所以我可以在我的视图模型中显示。
我有一个名为 QVRRepository.cs 的存储库类,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using VTracker.Contexts;
using Microsoft.Extensions.Logging;
namespace VTracker.Repositories
{
public class QVRRepository : IQVRRepository
{
// Declare variable to hold the QVRDataContext object to call using LINQ queries
private QVRDataContext _context;
private ILogger _logger;
// Constructor class to call and assign data context on init
public QVRRepository(QVRDataContext context, ILogger<QVRRepository> logger)
{
_context = context;
_logger = logger;
}
public class VulnerabilityCount
{
public string Owner { get; set; }
public List<KeyValuePair<string, int>> StatusCounts { get; set; }
};
public IEnumerable<VulnerabilityCount> GetCountsByStatus()
{
string owner;
List<KeyValuePair<string, int>> statusCounts = new List<KeyValuePair<string, int>>();
List<VulnerabilityCount> vulnerabilityCounts = new List<VulnerabilityCount>() { };
var query =
from o in _context.QVRData
group o by o.RamOwnedBy into ownerGroup
orderby ownerGroup.Key
select ownerGroup;
foreach (var group in query)
{
owner = group.Key;
Console.WriteLine(owner);
foreach (var o in group.GroupBy(o => o.AdditionalResourcesRequired))
{
statusCounts.Add(new KeyValuePair<string, int>(o.Key, o.Count()));
Console.WriteLine($"\t{o.Key} : {o.Count()}");
}
vulnerabilityCounts.Add(new VulnerabilityCount()
{
Owner = owner,
StatusCounts = statusCounts
});
}
return vulnerabilityCounts;
}
}
}
目的是返回类似于SQL查询的内容:
select [RAM Owned By], [Additional Resources Required], count([Additional Resources Required])
from QVRData
group by [RAM Owned By], [Additional Resources Required]
order by [RAM Owned By], [Additional Resources Required]
使用 QVRRepository.cs 中的代码,我正在向控制台写入结果,我看到结果显示为我想要的结果:
CBT-Platform Management-CCI
Researching - RRO Team - App Team Is Not Product Owner : 447
Blank : 4131
Researching - App Team - Not Yet Scheduled : 5
Compliant : 19
Researching - RRO Team - No Solution Available : 11
CBT-Platform Management-EBS
Researching - RRO Team - App Team Is Not Product Owner : 1045
Blank : 7995
Scheduled : 447
Compliant : 32
Researching - App Team - Not Yet Scheduled : 12
Blocked : 1
Researching - RRO Team - No Solution Available : 1
Exception Required in Policyworks : 3
但我正在努力弄清楚如何将这些结果编译为一组对象,以便我可以以类似的方式在我的视图中显示它们。
在我的* Index.cshtml **中,我有以下内容:
@model IEnumerable<VTracker.Repositories.QVRRepository.VulnerabilityCount>
@{
ViewData["Title"] = "QVR";
}
<div class="flex-container">
@foreach (var i in Model)
{
<div class="qvr-card">
<div class="qvr-card-header">
<p class="text-center">@i.Owner.Replace("CBT-", "").Replace("Platform Management", "Platform-MGMT")</p>
</div>
<div class="inner-container">
<div>
@foreach (var s in i.StatusCounts)
{
<div>@s.Key</div>
<div>@s.Value</div>
}
</div>
</div>
</div>
}
</div>
正如您在下面的屏幕截图中看到的那样,我确实搞砸了......
第一个foreach返回所有者值作为标题罚款,但不是仅显示&#34;所有者&#34;的状态计数,而是显示所有&的所有计数(状态+计数) #34;业主&#34;在每一个下:
答案 0 :(得分:3)
在您的GetCountsByStatus
方法中,您正在初始化statusCounts
超出for
循环。因此,vulnerabilityCounts
集合中的每个项目都与statusCounts
具有相同的引用。将其移至for
循环内:
foreach (var group in query)
{
// there is no need to keep this outside either
string owner = group.Key;
// for every owner, the statusCounts gets initialized again
List<KeyValuePair<string, int>> statusCounts = new List<KeyValuePair<string, int>>();
Console.WriteLine(owner);
foreach (var o in group.GroupBy(o => o.AdditionalResourcesRequired))
{
statusCounts.Add(new KeyValuePair<string, int>(o.Key, o.Count()));
Console.WriteLine($"\t{o.Key} : {o.Count()}");
}
vulnerabilityCounts.Add(new VulnerabilityCount()
{
Owner = owner,
StatusCounts = statusCounts
});
}