建立关系分组表达式树C#

时间:2019-04-10 16:04:20

标签: c# lambda group-by expression expression-trees

上下文:

使用Ag-Grid,用户应该能够拖放要分组的列。

enter image description here

假设我具有以下模型并按功能分组:

List<OrderModel> orders = new List<OrderModel>()
{
    new OrderModel()
    {
        OrderId = 184214,
        Contact = new ContactModel()
        {
            ContactId = 1000
        }
    }
};

var queryOrders = orders.AsQueryable();

编辑:因此,人们使我意识到,在以下问题中,我实际上是在动态Select上专注于正确的项目(这是要求之一),我错过了实际进行分组。因此,进行了一些编辑以反映这两个问题:分组和选择,强类型。

以类型定义的方式:

单列

IQueryable<OrderModel> resultQueryable = queryOrders
    .GroupBy(x => x.ExclPrice)
    .Select(x => new OrderModel() { ExclPrice = x.Key.ExclPrice});

多列

 IQueryable<OrderModel> resultQueryable = queryOrders
            .GroupBy(x => new OrderModel() { Contact = new ContactModel(){ ContactId = x.Contact.ContactId }, ExclPrice = x.ExclPrice})
            .Select(x => new OrderModel() {Contact = new ContactModel() {ContactId = x.Key.Contact.ContactId}, ExclPrice = x.Key.ExclPrice});

但是,最后一个不起作用,在OrderModel中定义GroupBy显然在将其转换为SQL时会出现问题。

如何使用表达式构建此GroupBy / Select

目前,我可以选择正确的项目,但尚未进行分组。

public static IQueryable<TModel> GroupByExpression(List<string> propertyNames, IQueryable<TModel> sequence)
{
    var param = Expression.Parameter(typeof(TModel), "item");
    var body = Expression.New(typeof(TModel).GetConstructors()[0]);
    var bindings = new List<MemberAssignment>();
    foreach (var property in propertyNames)
    {
        var fieldValue = typeof(TModel).GetProperty(property, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);

        var fieldValueOriginal = Expression.Property(param, fieldValue ?? throw new InvalidOperationException());

        var memberAssignment = Expression.Bind(fieldValue, fieldValueOriginal);
        bindings.Add(memberAssignment);
    }
    var result = sequence.Select(Expression.Lambda<Func<TModel, TModel>>(Expression.MemberInit(body, bindings), param));
    return result;
}

这很好,直到我想引入一段关系为止,因此在我的示例中,item.Contact.ContactId

我尝试过这种方式:

public static IQueryable<TModel> GroupByExpression(List<string> propertyNames, IQueryable<TModel> sequence)
{
    var param = Expression.Parameter(typeof(TModel), "item");
    Expression propertyExp = param;
    var body = Expression.New(typeof(TModel).GetConstructors()[0]);
    var bindings = new List<MemberAssignment>();
    foreach (var property in propertyNames)
    {
        if (property.Contains("."))
        {
            //support nested, relation grouping
            string[] childProperties = property.Split('.');
            var prop = typeof(TModel).GetProperty(childProperties[0], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase);
            propertyExp = Expression.MakeMemberAccess(param, prop);
            //loop over the rest of the childs until we have reached the correct property
            for (int i = 1; i < childProperties.Length; i++)
            {
                prop = prop.PropertyType.GetProperty(childProperties[i],
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase);
                propertyExp = Expression.MakeMemberAccess(propertyExp, prop);

                if (i == childProperties.Length - 1)//last item, this would be the grouping field item
                {
                    var memberAssignment = Expression.Bind(prop, propertyExp);
                    bindings.Add(memberAssignment);
                }
            }
        }
        else
        {
            var fieldValue = typeof(TModel).GetProperty(property, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);

            var fieldValueOriginal = Expression.Property(param, fieldValue ?? throw new InvalidOperationException());

            var memberAssignment = Expression.Bind(fieldValue, fieldValueOriginal);
            bindings.Add(memberAssignment);
        }


    }
    var memInitExpress = Expression.MemberInit(body, bindings);
    var result = sequence.Select(Expression.Lambda<Func<TModel, TModel>>(memInitExpress, param));
    return result;
}

可能看起来很有希望,但是不幸的是,它在var memInitExpress = Expression.MemberInit(body, bindings);上引发了错误

  

ArgumentException“ ContactId”不是“ OrderModel”类型的成员

这是在多列上分组时表达式的样子:

Expression.MemberInit(body, bindings)的结果是: {new OrderModel() {TotalInclPrice = item.TotalInclPrice, OrderId = item.OrderId}}

因此整个表达式为:{item => new OrderModel() {TotalInclPrice = item.TotalInclPrice, OrderId = item.OrderId}}

因此,现在很容易理解为什么我提到了我提到的异常,这仅仅是因为它使用OrderModel选择属性,而ContactId不在该模型中。但是,我受到限制并且必须坚持使用IQueryable<OrderModel>,所以现在的问题是如何使用同一模型创建要按ContactId分组的表达式。我想我实际上需要这样一个表达式:

Expression.MemberInit(body, bindings)的结果必须为: {new OrderModel() { Contact = new ContactModel() { ContactId = item.Contact.ContactId} , OrderId = item.OrderId}}。像这样吗?

因此,我想让我们回到基础知识上,逐步进行。最终,for循环创建以下表达式。 See my answer是如何解决这一部分的,Ivan's answer似乎已经以一种通用的方式解决了这一问题,但是我尚未测试该代码。但是,这还没有进行分组,因此在应用分组后,这些答案可能不再起作用。

仅供参考:仅提供列字段contact.contactId,AgGrid即可找到属性关系。因此,在加载数据时,它只会尝试查找该属性。我认为创建上面的表达式时,它将在Grid中工作。我现在也在尝试如何创建子MemberInit,因为我认为这是成功完成此任务的解决方案。

2 个答案:

答案 0 :(得分:2)

如果想法是动态创建嵌套的MemberInit选择器,则可以按照以下步骤进行操作:

public static class QueryableExtensions
{
    public static IQueryable<T> SelectMembers<T>(this IQueryable<T> source, IEnumerable<string> memberPaths)
    {
        var parameter = Expression.Parameter(typeof(T), "item");
        var body = parameter.Select(memberPaths.Select(path => path.Split('.')));
        var selector = Expression.Lambda<Func<T, T>>(body, parameter);
        return source.Select(selector);
    }

    static Expression Select(this Expression source, IEnumerable<string[]> memberPaths, int depth = 0)
    {
        var bindings = memberPaths
            .Where(path => depth < path.Length)
            .GroupBy(path => path[depth], (name, items) =>
            {
                var item = Expression.PropertyOrField(source, name);
                return Expression.Bind(item.Member, item.Select(items, depth + 1));
            }).ToList();
        if (bindings.Count == 0) return source;
        return Expression.MemberInit(Expression.New(source.Type), bindings);
    }
}

基本上以递归方式处理成员路径,按成员名称将每个级别分组,然后将该成员绑定到源表达式或源表达式的MemberInit上。

答案 1 :(得分:1)

此答案分为两部分:

  1. 创建一个GroupBy表达式,并确保使用相同的返回类型。
  2. 根据Select表达式的结果创建一个GroupBy表达式

选择和分组-非通用

因此,下面是完整的解决方案,但是为了让您对它的工作原理有所了解,请参阅这段代码,这是非通用版本编写的。分组的代码几乎相同,唯一的区别是在开头添加了一个Key.属性。

public static IQueryable<TModel> GroupByExpression(List<string> propertyNames, IQueryable<TModel> sequence)
{
    var param = Expression.Parameter(typeof(TModel), "item");
    Expression propertyExp = param;
    var body = Expression.New(typeof(TModel).GetConstructors()[0]);
    var bindings = new List<MemberAssignment>();
    var queryOrders = orders.AsQueryable();
    var orderBindings = new List<MemberAssignment>();

    //..more code was here, see question

    var orderParam = Expression.Parameter(typeof(OrderModel), "item");
    Expression orderPropertyExp = orderParam;
    var orderPropContact = typeof(OrderModel).GetProperty("Contact");
    orderPropertyExp = Expression.MakeMemberAccess(orderPropertyExp, orderPropContact);
    var orderPropContactId = orderPropContact.PropertyType.GetProperty("ContactId");
    orderPropertyExp = Expression.MakeMemberAccess(orderPropertyExp, orderPropContactId);

    var contactBody = Expression.New(typeof(ContactModel).GetConstructors()[0]);
    var contactMemerAssignment = Expression.Bind(orderPropContactId, propertyExp);
    orderBindings.Add(contactMemerAssignment);
    var contactMemberInit = Expression.MemberInit(Expression.New(contactBody, orderBindings);

    var orderContactMemberAssignment = Expression.Bind(orderPropContact, contactMemberInit);

    var orderMemberInit = Expression.MemberInit(Expression.New(typeof(OrderModel).GetConstructors()[0]), new List<MemberAssignment>() {orderContactMemberAssignment});

    //during debugging with the same model, I know TModel is OrderModel, so I can cast it
    //of course this is just a quick hack to verify it is working correctly in AgGrid, and it is!
    return (IQueryable<TModel>)queryOrders.Select(Expression.Lambda<Func<OrderModel, OrderModel>>(orderMemberInit, param));
}

所以现在我们需要以一种通用的方式做到这一点。

分组:

要以一种通用的方式进行分组,我发现this amazing post,他值得为开发这一部分付出大量的功劳。但是,我必须对其进行修改以确保它也支持子关系。在我的示例中:Order.Contact.ContactId

我首先编写了此递归方法以正确获取MemberAssignment绑定。

    /// <summary>
    /// Recursive get the MemberAssignment
    /// </summary>
    /// <param name="param">The initial paramter expression: var param =  Expression.Parameter(typeof(T), "item");</param>
    /// <param name="baseType">The type of the model that is being used</param>
    /// <param name="propEx">Can be equal to 'param' or when already started with the first property, use:  Expression.MakeMemberAccess(param, prop);</param>
    /// <param name="properties">The child properties, so not all the properties in the object, but the sub-properties of one property.</param>
    /// <param name="index">Index to start at</param>
    /// <returns></returns>
    public static MemberAssignment RecursiveSelectBindings(ParameterExpression param, Type baseType, Expression propEx, string[] properties, int index)
    {
        //Get the first property from the list.
        var prop = baseType.GetProperty(properties[index], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase);
        var leftProperty = prop;
        Expression selectPropEx = Expression.MakeMemberAccess(propEx, prop);
        //If this is the last property, then bind it and return that Member assignment
        if (properties.Length - 1 == index)
        {
            var memberAssignment = Expression.Bind(prop, selectPropEx);
            return memberAssignment;
        }

        //If we have more sub-properties, make sure the sub-properties are correctly generated.
        //Generate a "new Model() { }"
        NewExpression selectSubBody = Expression.New(leftProperty.PropertyType.GetConstructors()[0]);
        //Get the binding of the next property (recursive)
        var getBinding = RecursiveSelectBindings(param, prop.PropertyType, selectPropEx, properties, index + 1);

        MemberInitExpression selectSubMemberInit =
            Expression.MemberInit(selectSubBody, new List<MemberAssignment>() { getBinding });

        //Finish the binding by generating "new Model() { Property = item.Property.Property } 
        //During debugging the code, it will become clear what is what.
        MemberAssignment selectSubMemberAssignment = Expression.Bind(leftProperty, selectSubMemberInit);

        return selectSubMemberAssignment;
    }

然后,我可以修改post I mentionedSelect<T>方法:

    static Expression Select<T>(this IQueryable<T> source, string[] fields)
    {
        var itemType = typeof(T);
        var groupType = itemType; //itemType.Derive();
        var itemParam = Expression.Parameter(itemType, "x");


        List<MemberAssignment> bindings = new List<MemberAssignment>();
        foreach (var property in fields)
        {
            Expression propertyExp;
            if (property.Contains("."))
            {
                string[] childProperties = property.Split('.');
                var binding = RecursiveSelectBindings(itemParam, itemType, itemParam, childProperties, 0);
                bindings.Add(binding);
            }
            else
            {
                var fieldValue = groupType.GetProperty(property, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);
                var fieldValueOriginal = Expression.Property(itemParam, fieldValue ?? throw new InvalidOperationException());

                var memberAssignment = Expression.Bind(fieldValue, fieldValueOriginal);
                bindings.Add(memberAssignment);
            }
        }

        var selector = Expression.MemberInit(Expression.New(groupType), bindings.ToArray());
        return Expression.Lambda(selector, itemParam);
    }

上面的代码被下面的代码(我没有修改)调用,但是您可以看到它返回IQueryable<IGrouping<T,T>>类型。

    static IQueryable<IGrouping<T, T>> GroupEntitiesBy<T>(this IQueryable<T> source, string[] fields)
    {
        var itemType = typeof(T);
        var method = typeof(Queryable).GetMethods()
                     .Where(m => m.Name == "GroupBy")
                     .Single(m => m.GetParameters().Length == 2)
                     .MakeGenericMethod(itemType, itemType);

        var result = method.Invoke(null, new object[] { source, source.Select(fields) });
        return (IQueryable<IGrouping<T, T>>)result;
    }

选择

因此,我们现在完成了GroupBy表达式,我们现在需要做的是Select表达式。正如我之前所说的,它几乎等于GroupBy,唯一的区别是我们必须在每个属性的前面添加Key.。这是因为KeyGroupBy的结果,因此您需要从此开始。

    public static IQueryable<TModel> GroupByExpression(List<string> propertyNames, IQueryable<TModel> sequence)
    {
       var grouping = sequence.GroupBy(propertyNames.ToArray());

        var selectParam = Expression.Parameter(grouping.ElementType, "item");
        Expression selectPropEx = selectParam;
        var selectBody = Expression.New(typeof(TModel).GetConstructors()[0]);
        var selectBindings = new List<MemberAssignment>();
        foreach (var property in propertyNames)
        {
            var keyProp = "Key." + property;
            //support nested, relation grouping
            string[] childProperties = keyProp.Split('.');
            var prop = grouping.ElementType.GetProperty(childProperties[0], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase);
            selectPropEx = Expression.MakeMemberAccess(selectParam, prop);

            var binding = PropertyGrouping.RecursiveSelectBindings(selectParam, prop.PropertyType, selectPropEx, childProperties, 1);
            selectBindings.Add(binding);
        }

        MemberInitExpression selectMemberInit = Expression.MemberInit(selectBody, selectBindings);

        var queryable = grouping
            .Select(Expression.Lambda<Func<IGrouping<TModel, TModel>, TModel>>(selectMemberInit, selectParam));
        return queryable;

    }

GetHashCode()

不幸的是,直到我开始在所使用的每个模型中实现GetHasCode()Equals()为止,这仍然行不通。在Count()或通过执行.ToList()执行查询期间,它将比较所有对象以确保对象彼此相等(或不相等)。如果它们相等:同一组。但是因为我们是动态生成这些模型的,所以它没有一种方法可以根据内存位置正确地比较那些对象(默认情况下)。

幸运的是,您可以很容易地生成这两种方法:

https://docs.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods?view=vs-2019

确保至少包含了您将在表中使用的所有属性(并且可以对它们进行分组)。