我一直在引用this post使用表达式树进行分组。这是我的代码:
String[] fields = { "DepartmentID", "SkillID" };
var groupLambda = GroupByExpression<Person>(fields);
var query = dbContext.People.GroupBy(groupLambda.Compile());
var queryResult = query.ToList();
这里是 GroupByExpression
方法,它使用上述帖子中给出的解决方案(感谢 Daniel !):
public static Expression<Func<TItem, object>> GroupByExpression<TItem>(string[] propertyNames)
{
var properties = propertyNames.Select(name => typeof(TItem).GetProperty(name)).ToArray();
var propertyTypes = properties.Select(p => p.PropertyType).ToArray();
var tupleTypeDefinition = typeof(Tuple).Assembly.GetType("System.Tuple`" + properties.Length);
var tupleType = tupleTypeDefinition.MakeGenericType(propertyTypes);
var constructor = tupleType.GetConstructor(propertyTypes);
var param = Expression.Parameter(typeof(TItem), "x");
var body = Expression.New(constructor, properties.Select(p => Expression.Property(param, p)));
var expr = Expression.Lambda<Func<TItem, object>>(body, param);
return expr;
}
我希望能够通过选择部分中具有强名称的键来识别组中的字段,例如 query.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });
我该怎么做?
最佳答案
现在...我不会为您提供您提出的问题的解决方案,但我会尽力帮助您:-)
如果你想做动态查询,你可能应该使用 DynamicLinq
与 DynamicLinq你可以这样做:
IQueryable query = context.YourTable;
var groups = query.GroupBy("new (Field1, Field2)");
我正在重读你的问题...
I want to be able to identify fields in the group by keys with strong names in select part like query.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });
你不能。 GroupBy
一般会返回 IGrouping<TKey, TSource>
. TKey
是动态的(因为你基于字符串构建它),所以你不能“提取”它并将它传递给编译器,所以你不能使用强名称进行选择。
有一个异常(exception):如果您知道 GroupBy TKey 的类型和编号,那么就可以做一些事情。所以,你给了我们:
String[] fields = { "DepartmentID", "SkillID" };
如果您总是有两个 int 那么您可以使用以下方式进行查询:
.Cast<IGrouping<Tuple<int, int>, Person>>()
.Select(x => new { x.Key.DepartmentID, x.Key.SkillID });
请注意,正如我在评论中所写,您的 GroupBy
将在客户端执行,以及 GroupBy
之后的所有内容将在客户端执行(其中客户端 == 你的程序所在的位置 vs sql 端 == 你的 sql 服务器所在的位置)!
DynamicLinq
将解决执行查询sql端而不是客户端的问题,但是won't
解决强命名与弱命名的问题(在 DynamicLinq 之后,您可以:A)使用 .Cast<>()
方法或 B) 返回 dynamic
对象/IEnumerable<dynamic>
)
关于c# - 在 Group By 中使用表达式树后无法选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29120249/