亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 LINQ 或等效工具來格式化 List 對象的控制臺輸出

使用 LINQ 或等效工具來格式化 List 對象的控制臺輸出

C#
狐的傳說 2022-07-23 16:22:09
不確定如何做到這一點,猜測 C# LINQ 可能是可能的。所以我有一個對象:public class NContainer {   public string _HostFqdn { get; set; }   public string _HostIp { get; set; }   public int _Severity { get; set; }   public string _Issue { get; set; }   public string _ProtoPort { get; set; } }我給它一個如下列表:List<NContainer> nList = new List<NContainer>();nList.Add( new NContainer { _HostFqdn = "ab1.corp.com", _HostIp = "192.168.0.2", _Severity = 1, _Issue = "Check 1", _ProtoPort = "TCP_80" } );nList.Add( new NContainer { _HostFqdn = "ab2.corp.com", _HostIp = "192.168.0.3", _Severity = 2, _Issue = "Check 2", _ProtoPort = "TCP_81" } );nList.Add( new NContainer { _HostFqdn = "ab3.corp.com", _HostIp = "192.168.0.4", _Severity = 1, _Issue = "Check 2", _ProtoPort = "TCP_82" } );nList.Add( new NContainer { _HostFqdn = "ab4.corp.com", _HostIp = "192.168.0.5", _Severity = 3, _Issue = "Check 1", _ProtoPort = "TCP_80" } );nList.Add( new NContainer { _HostFqdn = "ab5.corp.com", _HostIp = "192.168.0.6", _Severity = 3, _Issue = "Check 5", _ProtoPort = "TCP_443" } );nList.Add( new NContainer { _HostFqdn = "ab6.corp.com", _HostIp = "192.168.0.7", _Severity = 4, _Issue = "Check 1", _ProtoPort = "TCP_80" } );我希望能夠在上面的列表中運行 LINQ 查詢(或類似查詢),以便控制臺輸出采用以下格式:Group By _Issue   Check 1    192.168.0.2 TCP_80   192.168.0.5 TCP_82   192.168.0.7 TCP_80   Check 2   192.168.0.3 TCP_81   192.168.0.4 TCP_82      Check 5   192.168.0.6 TCP_443我可以使用類似于下面的代碼顯示 List 和 orderby 的內容,但不知道如何以上面的格式顯示輸出?List<NContainer> arrList = new List<NContainer>();List<NContainer> query = from NContainer vulns in arrList                                orderby vulns._Issue                                where vulns._Severity >= 1                                select vulns;    foreach (var vuln in query)    {      Console.WriteLine("{0}", vuln._Issue, vuln._HostIp, vuln._ProtoPort);    }
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

您可以按 _Issue 分組并寫入控制臺,如下所示


var results = nList.GroupBy(x=>x._Issue);


Console.WriteLine("Group By _Issue");

foreach(var item in results)

{

    Console.WriteLine(item.Key);

    var IpString = string.Join("  ",item.ToList().Select(x=> $"{x._HostIp} {x._ProtoPort}"));

    Console.WriteLine(IpString);

    Console.WriteLine();

}

輸出


Group By _Issue

Check 1

192.168.0.2 TCP_80  192.168.0.5 TCP_80  192.168.0.7 TCP_80


Check 2

192.168.0.3 TCP_81  192.168.0.4 TCP_82


Check 5

192.168.0.6 TCP_443


查看完整回答
反對 回復 2022-07-23
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

可能有更好的方法來做到這一點,但這有效:


        List<NContainer> nList = new List<NContainer>();

        nList.Add(new NContainer { _HostFqdn = "ab1.corp.com", _HostIp = "192.168.0.2", _Severity = 1, _Issue = "Check 1", _ProtoPort = "TCP_80" });

        nList.Add(new NContainer { _HostFqdn = "ab2.corp.com", _HostIp = "192.168.0.3", _Severity = 2, _Issue = "Check 2", _ProtoPort = "TCP_81" });

        nList.Add(new NContainer { _HostFqdn = "ab3.corp.com", _HostIp = "192.168.0.4", _Severity = 1, _Issue = "Check 2", _ProtoPort = "TCP_82" });

        nList.Add(new NContainer { _HostFqdn = "ab4.corp.com", _HostIp = "192.168.0.5", _Severity = 3, _Issue = "Check 1", _ProtoPort = "TCP_80" });

        nList.Add(new NContainer { _HostFqdn = "ab5.corp.com", _HostIp = "192.168.0.6", _Severity = 3, _Issue = "Check 5", _ProtoPort = "TCP_443" });

        nList.Add(new NContainer { _HostFqdn = "ab6.corp.com", _HostIp = "192.168.0.7", _Severity = 4, _Issue = "Check 1", _ProtoPort = "TCP_80" });


        IEnumerable<NContainer> query = from NContainer vulns in nList

                                 orderby vulns._Issue

                                 where vulns._Severity >= 1

                                 select vulns;


        Console.WriteLine("Group by _Issue");

        var prevIssue = "";

        bool first = true;

        foreach (var vuln in query)

        {

            if (prevIssue != vuln._Issue)

            {

                if (first)

                    first = false;

                else

                    Console.WriteLine("\n");

                Console.WriteLine("\t{0}", vuln._Issue);

                Console.Write("\t");

                prevIssue = vuln._Issue;

            }

            Console.Write("{0} {1}  ", vuln._HostIp, vuln._ProtoPort);

        }

基本上使用 Console.Write 而不是 WriteLine 以便您可以循環并將特定信息的所有 IP 信息附加到同一行。其余的只是格式化。


查看完整回答
反對 回復 2022-07-23
  • 2 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號