私はプロパティ(別のリスト)のプロパティ(リスト)に基づいているオブジェクトのリストをソートする方法を見つけることを試みています。ここに私のオブジェクトです:並べ替えリスト<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
「cardz.OrderBy(a => a.CardRoles.Count)」だけではないのはなぜですか? – ASpirin
あなたはリストのリストを持っているので、あなたは内側のリストカウントによって何を意味するのかを定義する必要があります。どうやらこれは1つではないので、 'Sum'、' Min'、 'Max'などの集計を定義する必要があります。 –
あなたの例では、カードに複数のロールがある場合の動作は表示されません。 – NetMage