2017-09-14 7 views
2

私はプロパティ(別のリスト)のプロパティ(リスト)に基づいているオブジェクトのリストをソートする方法を見つけることを試みています。ここに私のオブジェクトです:並べ替えリスト<T>内部リストのプロパティの数で

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

結果:カードはDependentRolesの数によって記載されていますか

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 

お知らせ:

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 

そして、何私が見えるように結果を必要とするが、このです。これは達成できますか?もしそうなら、どうですか?アミットの答えを使用して

UPDATE

、私は正しい結果を得るが、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 
+0

「cardz.OrderBy(a => a.CardRoles.Count)」だけではないのはなぜですか? – ASpirin

+1

あなたはリストのリストを持っているので、あなたは内側のリストカウントによって何を意味するのかを定義する必要があります。どうやらこれは1つではないので、 'Sum'、' Min'、 'Max'などの集計を定義する必要があります。 –

+0

あなたの例では、カードに複数のロールがある場合の動作は表示されません。 – NetMage

答えて

1

私はあなたが必要だと思うがfollowing-(その後、私に知らせていない場合)

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

これはうまくいきますが、私が考慮しなかったことは、カードに複数のロールがある場合、ロールをDependentRole.Countで昇順でのみ表示することです。更新された質問とdomain.pc5.domainとdomain.pc3.domainという名前のカードを見て、今行っていることを表示します(降順)。 – Robert

+1

@Robert foreachは、降順でロールを順序付ける必要があります。 –

+1

違いがありました。助けてくれてありがとう! – Robert

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); 
関連する問題