猿问

使用 LINQ 或等效工具来格式化 List 对象的控制台输出

不确定如何做到这一点,猜测 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);

    }


狐的传说
浏览 96回答 2
2回答

精慕HU

您可以按 _Issue 分组并写入控制台,如下所示var results = nList.GroupBy(x=>x._Issue);Console.WriteLine("Group By _Issue");foreach(var item in results){&nbsp; &nbsp; Console.WriteLine(item.Key);&nbsp; &nbsp; var IpString = string.Join("&nbsp; ",item.ToList().Select(x=> $"{x._HostIp} {x._ProtoPort}"));&nbsp; &nbsp; Console.WriteLine(IpString);&nbsp; &nbsp; Console.WriteLine();}输出Group By _IssueCheck 1192.168.0.2 TCP_80&nbsp; 192.168.0.5 TCP_80&nbsp; 192.168.0.7 TCP_80Check 2192.168.0.3 TCP_81&nbsp; 192.168.0.4 TCP_82Check 5192.168.0.6 TCP_443

千巷猫影

可能有更好的方法来做到这一点,但这有效:&nbsp; &nbsp; &nbsp; &nbsp; List<NContainer> nList = new List<NContainer>();&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab1.corp.com", _HostIp = "192.168.0.2", _Severity = 1, _Issue = "Check 1", _ProtoPort = "TCP_80" });&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab2.corp.com", _HostIp = "192.168.0.3", _Severity = 2, _Issue = "Check 2", _ProtoPort = "TCP_81" });&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab3.corp.com", _HostIp = "192.168.0.4", _Severity = 1, _Issue = "Check 2", _ProtoPort = "TCP_82" });&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab4.corp.com", _HostIp = "192.168.0.5", _Severity = 3, _Issue = "Check 1", _ProtoPort = "TCP_80" });&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab5.corp.com", _HostIp = "192.168.0.6", _Severity = 3, _Issue = "Check 5", _ProtoPort = "TCP_443" });&nbsp; &nbsp; &nbsp; &nbsp; nList.Add(new NContainer { _HostFqdn = "ab6.corp.com", _HostIp = "192.168.0.7", _Severity = 4, _Issue = "Check 1", _ProtoPort = "TCP_80" });&nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<NContainer> query = from NContainer vulns in nList&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;orderby vulns._Issue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where vulns._Severity >= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select vulns;&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Group by _Issue");&nbsp; &nbsp; &nbsp; &nbsp; var prevIssue = "";&nbsp; &nbsp; &nbsp; &nbsp; bool first = true;&nbsp; &nbsp; &nbsp; &nbsp; foreach (var vuln in query)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (prevIssue != vuln._Issue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (first)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("\t{0}", vuln._Issue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("\t");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prevIssue = vuln._Issue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("{0} {1}&nbsp; ", vuln._HostIp, vuln._ProtoPort);&nbsp; &nbsp; &nbsp; &nbsp; }基本上使用 Console.Write 而不是 WriteLine 以便您可以循环并将特定信息的所有 IP 信息附加到同一行。其余的只是格式化。
随时随地看视频慕课网APP
我要回答