Build GroupBy expression tree with multiple fields

前端 未结 2 590
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 03:21

To dynamically generate a GroupBy expression, I am trying to build a Linq expression tree. The fields to group by are dynamic and can differ in number.

I use this co

2条回答
  •  爱一瞬间的悲伤
    2020-12-06 04:08

    That lambda expression builds a dictionary of grouping fields.
    Dictionary does not implement Equals() and GetHashCode(), so it groups them by reference equality.
    Since you always return a new dictionary, each item gets its own group.

    You need to change it to create a type that correctly implements Equals() and GetHashCode() for value equality.
    Ordinarily, you would have the compiler generate an anonymous type. However, you can't do that here since you don't know the type signature at compile-time.
    Instead, you can construct a Tuple<...>:

    Expression.New(
        Type.GetType("System.Tuple`" + fields.Length)
            .MakeGenericType(fields.Select(studentType.GetProperty), 
        fields.Select(f => Expression.PropertyOrField(itemParam, f))
    )
    

提交回复
热议问题