在C#中,可以使用LINQ的GroupBy方法來對集合進行分組操作,然后可以結合Where方法對分組進行篩選。以下是一些常用的技巧:
var groups = list.GroupBy(item => item.Category);
var groups = list.GroupBy(item => item.Category)
.Where(group => group.Key == "Category1");
var groups = list.GroupBy(item => item.Category)
.Where(group => group.Key == "Category1")
.SelectMany(group => group.Where(item => item.Price > 100));
var groups = list.GroupBy(item => item.Category)
.OrderBy(group => group.Key);
var groups = list.GroupBy(item => item.Category)
.Select(group => new {
Category = group.Key,
TotalPrice = group.Sum(item => item.Price)
});
通過結合GroupBy和Where等方法,可以實現靈活的分組篩選操作。需要根據具體的需求來選擇合適的方法來進行操作。