使用LINQ或等效语言来格式化列表对象的控制台输出

时间:2019-01-15 03:20:27

标签: c# linq arraylist

不确定如何做到这一点,猜测可能是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<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 个答案:

答案 0 :(得分:0)

可能有更奇妙的方法,但这可行:

        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信息附加到同一行。其余的只是格式化。

答案 1 :(得分:0)

您可以按_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