Skip to main content
 首页 » 编程设计

c#之Expression.Call groupBy 然后选择

2024年02月13日11jackei

我正在尝试使用表达式树来构建一对嵌套的组,但完全被 Select 及其对参数的期望所困扰。我最终想做的是通过表达式树构建它;

var queryNestedGroups = products.GroupBy(x => x.Category) 
                .Select(p => new 
                { 
                    key = p.Key, 
                    objects = p.ToList().GroupBy(y => y.Subcategory) 
                        .Select(y => new { key = y.Key, objects = y.ToList() }) 
                }) 
                .AsQueryable(); 

这是我到目前为止的尝试(产品是一个列表);

var data = Expression.Constant(products); 
var arg = Expression.Parameter(typeof(Product), "arg"); 
var nameProperty = Expression.PropertyOrField(arg, "Category"); 
 
var groupByLambda = Expression.Lambda<Func<Product, string>>(nameProperty, arg); 
var groupByExpression = Expression.Call( 
    typeof(Queryable), 
    "GroupBy", 
    new Type[] { typeof(Product), typeof(string) }, 
    data, 
    groupByLambda); 
 
var parameterExp = Expression.Parameter(typeof(IGrouping<string, Product>), "p"); 
var keyProp = Expression.PropertyOrField(parameterExp, "Key"); 
ConstructorInfo constructorInfo = typeof(object) 
    .GetConstructor(new[] { typeof(string), typeof(Product) }); 
 
Type anonymousResultType = new { Key = "abc", Values = new List<Product>() }.GetType(); 
var exp = Expression.New( 
            anonymousResultType.GetConstructor(new[] { typeof(string), typeof(List<Product>) }), 
            Expression.Constant("def"), 
            Expression.Constant(new List<Product>())); 
var selectLambda = Expression.Lambda(exp); 
 
var selectExpression = Expression.Call( 
    typeof(Queryable), 
    "Select", 
    new Type[] { typeof(List<Product>), selectLambda.Body.Type }, 
    data, 
    selectLambda);  
 
var finalExpression = Expression.Lambda(groupByExpression); 

一切都很顺利,除了我在 var selectExpression = ... 上得到异常,告诉我我的类型参数和参数是错误的。不幸的是,它没有告诉我哪些参数以及它们出错的原因。我在这里尝试了所有我能想到的排列方式。所以有两个问题;

我怎么知道是什么

  1. 它到底想要什么类型?
  2. 在这种情况下,正确的类型/参数是什么?

请您参考如下方法:

下面是执行您想要的操作的代码。每个选择都需要在表达式树中有自己的 lamda 投影。您还有两种不同的匿名类型,一种是内部匿名类型的 IEnumerable,另一种是产品列表。

此外,由于它是对象的 linq,您不需要 Queryable,您可以只使用 Enumerable 和 p.ToList().GroupBy(y => y.Subcategory) ToList 不需要,所以我没有转换它.

此外,如果您不使用匿名类型并使用具体类,它会更简单。特别是在最后。由于它不能是强类型的,因此您只需要编译它然后 DynamicInvoke 它。

// This could be a parameter 
var data = Expression.Constant(products); 
 
var outterGroupByarg = Expression.Parameter(typeof(Product), "x"); 
var outterGroupNameProperty = Expression.PropertyOrField(outterGroupByarg, "Category"); 
var outterGroupByLambda = Expression.Lambda<Func<Product, string>>(outterGroupNameProperty, outterGroupByarg); 
var outterGroupByExpression = Expression.Call(typeof(Enumerable), "GroupBy", new [] { typeof(Product), typeof(string) }, 
                                         data, outterGroupByLambda); 
 
var outterSelectParam = Expression.Parameter(typeof (IGrouping<string, Product>), "p"); 
 
var innerGroupByarg = Expression.Parameter(typeof(Product), "y"); 
var innerGroupNameProperty = Expression.PropertyOrField(innerGroupByarg, "Subcategory"); 
var innerGroupByLambda = Expression.Lambda<Func<Product, string>>(innerGroupNameProperty, innerGroupByarg); 
 
var innerGroupByExpression = Expression.Call(typeof(Enumerable), "GroupBy", new[] { typeof(Product), typeof(string) }, 
                                         outterSelectParam, innerGroupByLambda); 
 
var innerAnonymousType = new {Key = "abc", objects = new List<Product>()}; 
 
var innerSelectProjectionarg = Expression.Parameter(typeof(IGrouping<string, Product>), "y"); 
var innerKeyProp = Expression.Property(innerSelectProjectionarg, "Key"); 
 
var innerToList = Expression.Call(typeof (Enumerable), "ToList", new[] {typeof (Product)}, 
                                  innerSelectProjectionarg); 
 
var innerAnonymousResultType = innerAnonymousType.GetType(); 
var innerAnonymousConstructor = 
    innerAnonymousResultType.GetConstructor(new[] {typeof (string), typeof (List<Product>)}); 
var innerAnonymous = Expression.New(innerAnonymousConstructor, innerKeyProp, innerToList); 
 
var innerSelectProjection = 
    Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(IGrouping<string, Product>), innerAnonymousResultType), innerAnonymous, 
                      innerSelectProjectionarg); 
 
var innerSelectExpression = Expression.Call(typeof(Enumerable), "Select", new [] { typeof(IGrouping<string, Product>), innerAnonymousResultType }, 
                            innerGroupByExpression, innerSelectProjection); 
 
 
var outterAnonymousType = new {Key = "abc", Values = new[] {innerAnonymousType}.AsEnumerable()}; 
var outterAnonymousResultType = outterAnonymousType.GetType(); 
var outterAnonymousConstructor = 
    outterAnonymousResultType.GetConstructor(new[] { typeof(string), typeof(IEnumerable<>).MakeGenericType(innerAnonymousResultType) }); 
 
var outterKeyProp = Expression.PropertyOrField(outterSelectParam, "Key"); 
var outterAnonymous = Expression.New(outterAnonymousConstructor, outterKeyProp, innerSelectExpression); 
var outterSelectProjection = 
    Expression.Lambda( 
        typeof (Func<,>).MakeGenericType(typeof (IGrouping<string, Product>), outterAnonymousResultType), 
        outterAnonymous, 
        outterSelectParam); 
 
var outterSelect = Expression.Call(typeof(Enumerable), "Select", new[] { typeof(IGrouping<string, Product>), outterAnonymousResultType }, 
                                  outterGroupByExpression, outterSelectProjection); 
 
// Lamda is a func with no input because list of products was set as a constant and not a parameter 
var finial = 
    Expression.Lambda( 
        typeof (Func<>).MakeGenericType(typeof (IEnumerable<>).MakeGenericType(outterAnonymousResultType)), 
        outterSelect);