gpt4 book ai didi

c# - 如何返回 LINQ 结果并显示在 View 中?

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:27 24 4
gpt4 key购买 nike

我有一些类似的作品,但不确定如何将它们结合在一起以便在我的 View 模型中显示。

我有一个名为 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

但我正在努力弄清楚如何将这些结果编译为一组对象,以便我可以以类似的方式在我的 View 中显示它们。

在我的 *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 返回所有者值作为标题 fine,但不是只显示“所有者”的状态计数,而是显示每个“所有者”下的所有计数(状态 + 计数):

enter image description here

最佳答案

在您的 GetCountsByStatus 方法中,您正在 for 循环之外初始化 statusCounts。因此,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
});
}

关于c# - 如何返回 LINQ 结果并显示在 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46075740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com