在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等方法,可以实现灵活的分组筛选操作。需要根据具体的需求来选择合适的方法来进行操作。