简体   繁体   English

我如何对无序列表进行分组?

[英]How can i group unordered list?

How can i group unorderd List?我如何对无序列表进行分组? I have the following for-each to display RP informations but it creates a duplicate with the same Name, How can i group the li items by Name?我有以下 for-each 来显示 RP 信息,但它创建了一个具有相同名称的副本,我如何按名称对 li 项目进行分组?

<ul id="treeview">
     @foreach (var item in Info)
        {
          <li data-expanded="true">@item.PaName
                <ul>
                     <li data-expanded="true">@item.sName</li>
                 </ul>
           </li>
         }
  </ul>   

I want to see something like this我想看到这样的东西

----AOC
     ---DBSR
     ---KOLC

Just group data by PartyName, then you got data with key is PartyName and value is list of objects have the same PartyName.只需按 PartyName 对数据进行分组,然后您会得到键为 PartyName 的数据,值是具有相同 PartyName 的对象列表。 Use it to display in view instead.改用它在视图中显示。

You're on the right track talking about LINQ.您在谈论 LINQ 时走在正确的轨道上。 In a basic form your code could look like this:在基本形式中,您的代码可能如下所示:

using System.Linq

groupedData = data.GroupBy(x => x.PartyName)

The output will be in this structure output 将采用此结构

"AOC" : [
    {
        "SubPartyName": "DBSR",
        "OtherData": "...."
    },
    {
        "SubPartyName": "KOLC",
        "OtherData": "...."
    },
]

You could then loop through the grouped data in your view.然后,您可以遍历视图中的分组数据。

Let me know if you need more clarification如果您需要更多说明,请告诉我

I have the following for-each to display RP informations but it creates a duplicate with the same PartyName, How can i group the li items by PartyName?我有以下 for-each 来显示 RP 信息,但它创建了具有相同 PartyName 的副本,我如何按 PartyName 对 li 项目进行分组?

You can try following code snippet:您可以尝试以下代码段:

@model IEnumerable<Party>
@{
    ViewData["Title"] = "RParty";
    var rpInfo = Model.GroupBy(p=>p.PartyName);
}

<h1>RP Information</h1>

<ul id="treeview">
    @foreach (var item in rpInfo)
    {
        <li data-expanded="true">
            <i class="fa fa-check-circle"></i>@item.Key

            @foreach (var party in @item)
            {
                <ul>
                    <li data-expanded="true">
                        <i class="fas fa-check-circle"></i> @party.SubPartyName
                    </li>
                </ul>
            }

        </li>
    }
</ul> 

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

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