How to implement SelectMany in System.Linq.Dynamic ExpressionParser

扶醉桌前 提交于 2019-12-24 16:56:12

问题


I'm trying to implement the SelectMany statement inside of the dynamic linq expresion parser, such that I could run a query like so:

Customers.Select("Orders.SelectMany(OrderItems)")

Such that it would be equivilent to the linq query:

Customers.Select(cust => cust.Orders.SelectMany(ord => ord.OrderItems))

I've tried adding SelectMany to the IEnumerableSignatures of System.Linq.Dynamic.ExpressionParser, but it looks like there's more I need to do.

I've looked into this codeplex project but didn't get anywhere with it: http://dynamiclinq.codeplex.com/ specificaly it wouldn't run my old queries and didn't have support for select or select many.

Ultimately I'd like to use all of the ienumerable linq statements inside of a dynamic linq statement.


回答1:


For make SelectMany work inside dynamic linq queries you shoud not only modify IEnumerableSignatures, but also change ParseAggregate method to pass specific typeArgs for SelectMany, in this way:

    Expression ParseAggregate(Expression instance, Type elementType, string methodName, int errorPos)
    {
        ...
        Type[] typeArgs;
        if (signature.Name == "Min" || signature.Name == "Max")
        {
            typeArgs = new Type[] { elementType, args[0].Type };
        }
        else if(signature.Name == "Select")
        {
            typeArgs = new Type[] { elementType, Expression.Lambda(args[0], innerIt).Body.Type };
        } 
        else if(signature.Name == "SelectMany")
        {
            var type = Expression.Lambda(args[0], innerIt).Body.Type;
            var interfaces = type.GetInterfaces().Union(new[] { type });
            Type resultType = interfaces.Single(a => a.Name == typeof(IEnumerable<>).Name).GetGenericArguments()[0];
            typeArgs = new Type[] { elementType, resultType };
        }
        else
        {
            typeArgs = new Type[] { elementType };
        }
        ...
    }


来源:https://stackoverflow.com/questions/20269475/how-to-implement-selectmany-in-system-linq-dynamic-expressionparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!