簡體   English   中英

如何返回LINQ結果並在視圖中顯示?

[英]How to Return LINQ results and display in 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

但我正在努力弄清楚如何將這些結果編譯為一組對象,以便我可以以類似的方式在我的視圖中顯示它們。

在我的* 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返回所有者值作為標題罰款,但不是僅顯示“所有者”的狀態計數,而是顯示每個下所有“所有者”的所有計數(狀態+計數):

在此輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM