Linq:使用表达式树语法按多列分组

时间:2015-11-04 14:07:58

标签: c# linq expression-trees anonymous-types

我想更改以下代码,以便处理超过1个属性的分组

private Expression<Func<ProfileResultView, string>> DynamicGroupBy(string propertyName)
{
    var parameterExp = Expression.Parameter(typeof(ProfileResultView), "x");
    var memberExp = Expression.PropertyOrField(parameterExp, propertyName);
    return Expression.Lambda<Func<ProfileResultView, string>>(memberExp, parameterExp);
}

所以这将被翻译为

GroupBy(x => new { x.Column1, x.Column2 })

如何在表达式树语法中编写匿名类型?

1 个答案:

答案 0 :(得分:0)

如果分组键的类型对您无关紧要,您可以动态创建类型并根据这些类型调用分组:

public static Expression<Func<TSource, object>> DynamicGroupBy<TSource>
       (params string[] properties)
{
    var entityType = typeof(TSource);
    var props = properties.Select(x => entityType.GetProperty(x)).ToList();
    var source = Expression.Parameter(entityType, "x");

    // create x=> new myType{ prop1 = x.prop1,...}
    var newType = CreateNewType(props);
    var binding = props.Select(p => Expression.Bind(newType.GetField(p.Name), 
                  Expression.Property(source, p.Name))).ToList();
    var body = Expression.MemberInit(Expression.New(newType), binding);
    var selector = Expression.Lambda<Func<TSource, object>>(body, source);
   return selector;
}
public static Type CreateNewType(List<PropertyInfo> props)
{
   AssemblyName asmName = new AssemblyName("MyAsm");
   AssemblyBuilder dynamicAssembly = AssemblyBuilder
       .DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
   ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("MyAsm");
   TypeBuilder dynamicAnonymousType = dynamicModule
       .DefineType("MyType", TypeAttributes.Public);

   foreach (var p in props)
   {
       dynamicAnonymousType.DefineField(p.Name, p.PropertyType, FieldAttributes.Public);
   }
   return dynamicAnonymousType.CreateType();
}

请注意,组密钥类型为object