简体   繁体   English

如何在组中开始和结束时使用文本对linq进行分组?

[英]How can I do grouping in linq with text at the start and end of a group?

I have topic data looking like this: 我有这样的主题数据:

010000 <- Top level header
010100 A <- Sub level header
010101 B <- Clickable item in the select list
010102 C <- Clickable item in the select list
010103 D <- Clickable item in the select list
010200 E <- Sub level header
010201 F <- Clickable item in the select list
010202 G <- Clickable item in the select list

Currently I am using the following code to make a select list that shows everthing: 目前我使用以下代码制作一个显示everthing的选择列表:

var topics = contentService.Get(accountID + "06000").OrderBy(o => o.Order);
foreach (Content topic in topics) {
    txt += "<option value='" + topic.RowKey + "'>" + topic.Name + "</option>\n";
}

Is there a way I can change this so that: 有没有办法可以改变这个:

  • The top level headers are not part of the select list? 顶级标题不是选择列表的一部分? in other words every row that ends in "0000" is not put into the topics variable. 换句话说,以“0000”结尾的每一行都不会放入主题变量中。
  • The sub level headers appear as groups in the select list. 子级别标题在选择列表中显示为组。

Groups like this: 像这样的团体:

<select>
  <optgroup label="A">
      <option value="010101">B</option>
      <option value="010102">C</option>
      <option value="010103">D</option>
  </optgroup>
  <optgroup label="E">
      <option value="010201">F</option>
      <option value="010202">G</option>
  </optgroup>
</select>

I hope someone can help. 我希望有人能帮帮忙。 I think maybe I can do the limiting but I don't know how to do the start and end grouping. 我想也许我可以做限制但我不知道如何进行开始和结束分组。

Well, it's not really clear what's RowKey. 好吧,RowKey的含义并不是很清楚。 But let's say RowKey is "01xxxx". 但是我们说RowKey是“01xxxx”。 Don't know if "A, B, C..." are part of the same string or not... 不知道“A,B,C ......”是否是同一串的一部分......

var groupedList = topics.Where(m => m.RowKey.Substring(2, 2) != "00")
                       .GroupBy(m => m.RowKey.Substring(2, 2))
                       .ToList();

then you can use it like 然后就可以使用它了

var select = new XElement("select");
foreach (var group in groupedList)  {
   var subLevel = group.First();
   var optGroup = new XElement("optGroup", new XAttribute("label", subLevel.Name);
   optGroup.
   sb.Append(
   foreach (var item in group.Skip(1).ToList()) {
      optGroup.Add(new XElement("option", new XAttribute("value", item.RowKey), new XText(item.Name)));
   }
   select.Add(optGroup);
}
var result = select.ToString();

and if you have others groups starting with "02", "03" etc, you'll have to do a first grouping on topic.Substring(0, 2) . 如果你有其他以“02”,“03”等开头的组,你将不得不对topic.Substring(0, 2)进行第一次分组。

To simplify matters a bit, let's assume that your topics are defined in a following List : 为了简化一些事项,我们假设您的主题在以下List中定义:

List<Tuple<string, string>> topics = new List<Tuple<string, string>>
{
    Tuple.Create("010000", string.Empty),
    Tuple.Create("010100", "A"),
    Tuple.Create("010101", "B"),
    Tuple.Create("010102", "C"),
    Tuple.Create("010103", "D"),
    Tuple.Create("010200", "E"),
    Tuple.Create("010201", "F"),
    Tuple.Create("010202", "G"),
};

Then, you could simply iterate over each topic and build your HTML step-by-step: 然后,您可以简单地遍历每个主题并逐步构建HTML:

XElement select = new XElement("select");
XElement optGroup = null;

foreach (var topic in topics)
{
    // skip root topic
    if (!topic.Item1.EndsWith("0000"))
    {
        // optgroup
        if (topic.Item1.EndsWith("00"))
        {
            optGroup = new XElement("optgroup", new XAttribute("label", topic.Item2));
            select.Add(optGroup);
        }
        // option
        else if (optGroup != null)
        {
            optGroup.Add(new XElement("option", new XAttribute("value", topic.Item1), new XText(topic.Item2)));
        }
    }
}

Console.WriteLine(select);

You'll get a following console output: 您将获得以下控制台输出:

<select>
    <optgroup label="A">
        <option value="010101">B</option>
        <option value="010102">C</option>
        <option value="010103">D</option>
    </optgroup>
    <optgroup label="E">
        <option value="010201">F</option>
        <option value="010202">G</option>
    </optgroup>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM