我有以下示例類。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 }
}
},
};
我如何使用LINQ訂購Parent
對象由Child
的價格與具體類別,說‘第3項’?
你的意思是這樣的嗎? parent.OrderBy(x => x.Children.Where(y => y.Category ==「Item 3」)。Max(z => z.Price)); –
這樣做,謝謝。隨意發佈作爲答案,我會接受。 – erdinger