Вложенный выбор многих/Выберите LINQ DynamicQueryable

#c# #linq #dynamicquery

#c# #linq #динамический запрос

Вопрос:

Как мне преобразовать этот запрос LINQ в динамический запрос?

 var resultList = itemList.SelectMany(b => b.SubItemList.Select(x => new {b.Supplier,
b.Customer, x.ProductCode, x.ProductPrice}));
  

У меня есть этот метод:

 public static IQueryable SelectMany(this IQueryable source, string selector, params     object[] values)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (selector == null)
            throw new ArgumentNullException("selector");

        // Parse the lambda
        LambdaExpression lambda =
            DynamicExpression.ParseLambda(source.ElementType, null, selector, values);

        // Fix lambda by recreating to be of correct Func<> type in case 
        // the expression parsed to something other than IEnumerable<T>.
        // For instance, a expression evaluating to List<T> would result 
        // in a lambda of type Func<T, List<T>> when we need one of type
        // an Func<T, IEnumerable<T> in order to call SelectMany().
        Type inputType = source.Expression.Type.GetGenericArguments()[0];
        Type resultType = lambda.Body.Type.GetGenericArguments()[0];
        Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
        Type delegateType = typeof(Func<,>).MakeGenericType(inputType, enumerableType);
        lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);

        // Create the new query
        return source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "SelectMany",
                new Type[] { source.ElementType, resultType },
                source.Expression, Expression.Quote(lambda)));
    }
  

и я мог бы использовать это как

 var resultList = itemList.SelectMany("SubItemList");
  

но как насчет выбора внутри моего SelectMany? как мне создать вложенный динамический запрос?
Спасибо!