【问题标题】:Sort List<T> by count of property of inner list按内部列表的属性计数对 List<T> 进行排序
【发布时间】:2017-09-15 02:59:24
【问题描述】:

我正在尝试找到一种方法来根据属性(另一个列表)的属性(这是一个列表)对我拥有的对象列表进行排序。这是我的对象:

电脑卡:

public partial class ComputerCard : XtraUserControl
{
    public string ComputerName { get; set; }
    public List<Role> CardRoles = new List<Role>();
    //More properties that are omitted for brevity.
}

角色:

public class Role
{
    public string RoleName { get; set; }
    public string ImageName { get; set; }
    public List<DependentRole> DependentRoles { get; set; }
    public List<AppToRun> AppsToRun { get; set; }

    public Role()
    {
        if (AppsToRun == null)
            AppsToRun = new List<AppToRun>();

        if (DependentRoles == null)
            DependentRoles = new List<DependentRole>();
    }
}

依赖角色:

public class DependentRole
{
    public string Name { get; set; }
}

在我的主要形式中,我声明了以下内容:

List<ComputerCard> cards

如您所见,cards 可以有 1 个或多个 Role,每个 Role 可以有 0 个或多个 DependentRole。因此,鉴于此列表,我需要一种按DependentRole.Count 降序排列的方法。我查看并尝试了此 SO Link - How to sort list of list by count? 的推荐,但运气不佳。

List<ComputerCard> cardz = cards.Where(c => c.LookUpEdit.EditValue != null).ToList();
cardz.Sort((a, b) => a.CardRoles.Count - b.CardRoles.Count);

结果:

Card: File Server
  Role: Server
    Dependent Roles: 0

Card: VCS Gateway
  Role: File Server
    Dependent Roles: 0

Card: domain.pc1.domain
  Role: Pilot 1
    Dependent Roles: 3

Card: domain.pc5.domain
  Role: Local Controller
    Dependent Roles: 1

Card: domain.pc3.domain
  Role: Pilot 3
    Dependent Roles: 3

Card: domain.pc4.domain
  Role: Ground Controller
    Dependent Roles: 2

我需要的结果是这样的:

Card: File Server
  Role: Server
    Dependent Roles: 0

Card: VCS Gateway
  Role: File Server
    Dependent Roles: 0

Card: domain.pc5.domain
  Role: Local Controller
    Dependent Roles: 1

Card: domain.pc4.domain
  Role: Ground Controller
    Dependent Roles: 2

Card: domain.pc1.domain
  Role: Pilot 1
    Dependent Roles: 3

Card: domain.pc3.domain
  Role: Pilot 3
    Dependent Roles: 3

请注意卡片是如何按 DependentRoles 计数列出的。这可以实现吗?如果有,怎么做?

更新

使用 Amit 的答案,我得到了正确的结果,但忽略了在 DependentRoles.Count 上按升序列出具有多个角色的卡的要求:

Card: File Server
    Role: Server
        Dependent Roles: 0

Card: VCS Gateway
    Role: File Server
        Dependent Roles: 2

Card: domain.pc1.domain
    Role: Pilot 1
        Dependent Roles: 3

Card: domain.pc4.domain
    Role: Pilot 1
        Dependent Roles: 3

Card: domain.pc2.domain
    Role: Pilot 1
        Dependent Roles: 3

Card: domain.pc5.domain
    Role: Pilot 1
        Dependent Roles: 3
    Role: Ground Controller
        Dependent Roles: 2

Card: domain.pc3.domain
    Role: Pilot 1
        Dependent Roles: 3
    Role: Ground Controller
        Dependent Roles: 2
    Role: VCS Gateway
        Dependent Roles: 0

【问题讨论】:

  • 为什么不只是cardz.OrderBy(a =&gt; a.CardRoles.Count)
  • 既然您有列表列表,您需要定义内部列表计数是什么意思。显然它不仅仅是一个,所以你必须定义一些聚合,比如SumMinMax等。
  • 您的示例没有显示卡片具有多个角色时会发生什么。

标签: c# list linq


【解决方案1】:

我认为您需要以下内容-(如果没有,请告诉我)

List<ComputerCard> SortedCards = cards.OrderBy(                    
                   x => x.CardRoles.Sum(y => y.DependentRoles.Count))
                   .ToList();
foreach (var item in SortedCards)
{
    item.CardRoles = item.CardRoles
           .OrderByDescending(x => x.DependentRoles.Count).ToList();
}

【讨论】:

  • 这很好用,但我没有考虑到的一件事是如果卡片中有多个角色,那么我希望角色仅按升序由 DependentRole.Count 列出.有关结果,请参阅我更新的问题,名为 domain.pc5.domain 和 domain.pc3.domain 的卡片将向您展示它现在在做什么(降序)。
  • @Robert foreach 现在应该按降序排列角色。
  • 这有所不同。感谢您的帮助!
【解决方案2】:

试试这个:

 var ordered = cards.SelectMany(c => c.CardRoles.Select(r => new { Card = c, Role = r }))
      .OrderBy(a => a.Role.DependentRoles.Count)
      .ThenBy(a => a.Card.ComputerName)
      .ThenBy(a => a.Role.RoleName)
      .Select(c => c.Card);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2012-08-22
    • 2020-03-28
    • 2020-08-03
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多