【问题标题】:How do I generate an html string from group by如何从 group by 生成 html 字符串
【发布时间】:2015-02-26 14:49:32
【问题描述】:

这里有一个来自我班的 Assinantes 的列表

new Assinante
            {
                City= "BAURU",
                Num= 112,
                ClientCode= 3123,
                Phone= "1412345675"
            },
            new Assinante
            {
                City= "BAURU",
                Num= 45,
                ClientCode= 3123,
                Phone= "214464347"
            }

我需要按 City、ClientCod 和 Num 分组,我已经在这里完成了:

var listGroup= (from a in lista
            group a by new {a.City, a.ClientCode, a.Num});

然后,我需要用 Linq 生成一个 html 字符串,如下例所示:

<div>
   <h2>Bauru</h2>
   <ul>
        <li>3123</li>
        <ul>
            <li>112</li>
            <ul>
                <li>1412345675</li>
            </ul>
            <li>45</li>
            <ul>
                <li>214464347</li>
            </ul>
        </ul>
    </ul>
</div>

有人可以给我任何建议吗?

【问题讨论】:

  • 为什么要使用 C# 构建 html 字符串?只是问,因为根据您的问题,可能会有更好的解决方案
  • 因为我会用这个html发邮件给客户

标签: c# linq


【解决方案1】:

您可以使用 linq to xml 来解决这个问题,也包含在示例选项中以添加属性,这可能在将来有用(样式或查询)

var html =  new XElement("div", new XAttribute("class","dynamic-content"), 
                    from i in lst.GroupBy(x=>new{x.City,x.ClientCode,x.Num}) select 
                        new XElement("div",new XAttribute("class","city"),
                            new XElement("h1",new XAttribute("class","city-name"), i.Key.City ),
                            new XElement("ul",
                                from k in i.GroupBy(a=>a.ClientCode) select
                                        new XElement("li", 
                                            new XElement("h4",new XAttribute("class","client-code"), k.Key),
                                            new XElement("ul",
                                                from j in k.GroupBy(a=>a.Num) select 
                                                    new XElement("li", 
                                                        new XAttribute("class","client-num"), j.Key ,
                                                        new XElement("ul", new XAttribute("class","phone-numbers"), 
                                                            from l in j select 
                                                                new XElement("li", new XAttribute("class","phone-number"), l.Phone)
                                                    )
                                                )
                                            )
                                        )
                            )
                        )
            );

要获取实际字符串,只需使用html.ToString()

见小提琴here

【讨论】:

  • 使用XElement非常好,但结果与html不同,有问题
  • @ASh 输出有何不同?会有解决方法吗?没有机会测试输出,所以额外的见解会很有用
  • 开始,thml是IEnumerable&lt;XElement&gt;();string.Join(Environment.NewLine,html)等于

    BAURU

    • 112
      • 1412345675

    BAURU

    • 45
      • 214464347
  • @konkked 感谢您帮助我,但在您的回答中错过了 ClientCode。我不知道如何使用那个 XElement,在此先感谢。对不起,英语不好。
  • @MauroMaciel 感谢您的提醒,进行了一些更改以将类也包含到 html 中
【解决方案2】:

您可以使用XElement 生成所需的html。我想我的示例可以用 Linq 方法 Aggregate 重写,但我更喜欢嵌套 foreach,因为代码更易读

fiddle with demo

using System.Xml;
using System.Xml.Linq;
...
        var xml = new XElement("div");

        foreach(var city in lst.GroupBy(x=>x.City))
        {
            var cityXml = new XElement("h2", city.Key);
            var cityUl = new XElement("ul");                
            foreach(var client in city.GroupBy(c=>c.ClientCode))
            {
                var clientXml = new XElement("li", client.Key);
                var clientUl = new XElement("ul");

                foreach(var num in client.GroupBy(cl=>cl.Num))
                {
                    var numXml = new XElement("li", num.Key);
                    var numUl = new XElement("ul");                     
                    foreach(var phone in num)
                    {
                        numUl.Add(new XElement("li",phone.Phone));
                    }
                    clientUl.Add(numXml);
                    clientUl.Add(numUl);
                }                   
                cityUl.Add(clientXml);
                cityUl.Add(clientUl);
            }
            xml.Add(cityXml);
            xml.Add(cityUl);
        }

        string res = xml.ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多