我想用特定对象的公共属性填充下拉列表,我做得很好。但是现在当用户从下拉列表中选择值时,我希望它按该列对数据库表结果进行分组。我尝试过使用 LINQ,但我只能弄清楚如何通过实例变量属性而不是反射属性显式分组。这是我的方法——传入的参数是属性的字符串名称。例如,如果用户想要按 Customer.Country 分组,它将是“Country”,如果用户想要按 Customer.State 分组,它将是“State”。但目前我已经硬编码为按“状态”分组,因为我无法弄清楚如何使用通过我的 LINQ 查询传入的字符串值
private void DisplayReportAction(string category)
{
if (!string.IsNullOrEmpty(category))
{
SelectedCategory = category;
_summaries.Clear();
foreach (var custGroup in _customerInterface.CustomerInterface.GetAllCustomers().GroupBy(c => c.State)
.Select(group => new
{
Category = group.Key,
Count = group.Count()
})
.OrderBy(x => x.Category))
{
_summaries.Add(new CustomerReportSummaryViewModel(custGroup.Category, custGroup.Count));
}
ReportVisibility = Visibility.Visible;
}
}