我正在尝试使用表达式树来构建一组嵌套的组,并且完全被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();
这是我到目前为止的尝试(产品是List);
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 =上得到例外...告诉我我的类型参数和参数是错误的。不幸的是,它并没有告诉我哪些参数以及它们为什么是错误的。我已经尝试了我能想到的每一种排列..所以有两个问题;
我如何弄清楚
答案 0 :(得分:4)
以下是您想要的代码。每个选择都需要在表达式树中拥有它自己的lamda投影。您还有两种不同的匿名类型,一种是内部匿名类型的IEnumerable,另一种是产品列表。
此外,由于它不需要使用可查询对象,因此您只需使用Enumerable和p.ToList()。GroupBy(y =&gt; y.Subcategory)ToList isn&#39;我需要,所以我没有转换它。
如果你没有使用匿名类型并且具有具体的类,那么它也会更简单。特别是在最后。因为它不能强类型,所以你只需要编译它然后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);