按内部列表的属性计数对列表<t>进行排序

时间:2017-09-14 17:44:58

标签: c# list linq

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

ComputerCard:

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>();
    }
}

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上按升序排列多个角色的卡片的要求。

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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

试试这个:

 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);