次のサンプルクラスがあります。OrderBy子コレクション固有のカテゴリ
public class Parent
{
public int Id { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public string Category { get; set; }
public decimal? Price { get; set; }
}
および以下のサンプルデータ。
var parents = new List<Parent>()
{
new Parent() {
Id = 1,
Children = new List<Child>
{
new Child {Category = "Item 1", Price = 10 },
new Child {Category = "Item 2", Price = 30 },
new Child {Category = "Item 3", Price = null },
new Child {Category = "Item 4", Price = 5 },
new Child {Category = "Item 5", Price = 20 }
}
},
new Parent() {
Id = 2,
Children = new List<Child>
{
new Child {Category = "Item 1", Price = null },
new Child {Category = "Item 2", Price = null },
new Child {Category = "Item 3", Price = null },
new Child {Category = "Item 4", Price = null },
new Child {Category = "Item 5", Price = null }
}
},
new Parent() {
Id = 3,
Children = new List<Child>
{
new Child {Category = "Item 1", Price = 100 },
new Child {Category = "Item 2", Price = 300 },
new Child {Category = "Item 3", Price = null },
new Child {Category = "Item 4", Price = 50 },
new Child {Category = "Item 5", Price = 200 }
}
},
};
私は特定のカテゴリにChild
の価格でParent
オブジェクトを注文するLINQを使用することができますどのように、 『項目3』を言うの?
あなたはこのような何かを意味ですか? parents.OrderBy(x => x.Children.Where(y => y.Category == "Item 3")。Max(z => z.Price)); –
それは、ありがとう、それをしました。私は答えとして自由に投稿してください、私は受け入れます。 – erdinger