为了账号安全,请及时绑定邮箱和手机立即绑定

使用 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 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号