8

我想用特定对象的公共属性填充下拉列表,我做得很好。但是现在当用户从下拉列表中选择值时,我希望它按该列对数据库表结果进行分组。我尝试过使用 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;
    }
}
4

2 回答 2

21

如果您使用 LINQ to Objects,则可以使用反射,例如,您可以使用:

_customerInterface.CustomerInterface.GetAllCustomers()
     .GroupBy(c => c.GetType().GetProperty(category).GetValue(c, null))

如果您使用的是 Linq To Sql,那么另一种方法是使用动态查询,请查看此链接

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

于 2013-10-23T11:55:45.253 回答
3

您可以动态构建表达式:

    Expression<Func<Customer,T>> buildExpression<T>(string category)
    {
        //First build parameter of lambda ( 'c =>' part of expression)
        ParameterExpression param = Expression.Parameter(typeof(Customer), "c");
        //Then body of expression : ' => c.category'
        Expression<Func<Customer, T>> exp = Expression.Lambda<Func<Customer, T>>(Expression.Property(param, category), param);
        return exp;
    }

最后,打电话

_customerInterface.CustomerInterface.GetAllCustomers()
    .GroupBy(buildExpression(category))

编辑: 好吧,对不起,您仍然必须知道属性的类型才能将 T 类型参数赋予buildExpression功能

有一些方法可以做到这一点,例如使用GetProperty(category).PropertyType然后调用MakeGenericMethodon GetType().GetMethod("buildExpression<>"),但这需要更多的工作。无论如何,您必须找到一种CustomerReportSummaryViewModel从这种类型构建的方法。

我不知道您的用例,但是您可能所有类别属性都属于同一类型,因此您可以对其进行硬编码吗?

如果您有兴趣,但找不到合适的方法,请告诉我,我会尝试编写合适的解决方案。

于 2013-10-23T12:13:07.817 回答