Hubert R. Skrzypek
Hubert R. Skrzypek

Reputation: 95

How to combine result of multiple Linq Expressions into one Expression?

I have a list of Linq Expressions List<Expression> where each expression type (the type the expression would return) is either Item or Item[].

I'm trying to write some code that would take mentioned collection as an input parameter and produce a Linq Expression that would return one list (or array) of items (Item[]).

Here is an abstract example:

public static string[] GetStrings()
{
    return new[]
        {
            "first",
            "second",
            "third"
        };
}

public static string GetString()
{
    return "single1";
}

private void SOExample()
{
    var expressions = new List<Expression>
    {
        Expression.Call(GetType().GetMethod("GetString")),
        Expression.Call(GetType().GetMethod("GetStrings")), 
        Expression.Call(GetType().GetMethod("GetString")),
        Expression.Call(GetType().GetMethod("GetStrings"))
    };

    // some magic code here
    var combined = SomeMagicHere(expressions);
}


private Expression SomeMagicHere(List<Expression> expressions)
{
    foreach (var expression in expressions)
    {
        if (expression.Type.IsArray)
        {
            // Use array's elements
        } 
        else
        {
            // Use expression
        }
    }

What I'm trying to make is to produce one Expression that would return a list of Item (strings in my example) from provided list.

Upvotes: 1

Views: 1708

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064134

This seems a very odd scenario, and in most cases I would expect to see use of raw reflection (or maybe delegates) rather than Expression here - it isn't an obvious fit. But: you can do that by turning the expressions into a block that each call Add or AddRange to append values into the list. For example:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

static class Program
{
    public static string GetString()
    {
        return "single1";
    }
    public static string[] GetStrings()
    {
        return new[]
        {
            "first",
            "second",
            "third"
        };
    }
    static void Main()
    {
        var expressions = new List<Expression>
        {
            Expression.Call(typeof(Program).GetMethod("GetString")),
            Expression.Call(typeof(Program).GetMethod("GetStrings")), 
            Expression.Call(typeof(Program).GetMethod("GetString")),
            Expression.Call(typeof(Program).GetMethod("GetStrings"))
        };

        // some magic code here
        var combined = SomeMagicHere(expressions);

        // show it works
        var lambda = Expression.Lambda<Func<List<string>>>(combined);
        var list = lambda.Compile()();
    }
    private static Expression SomeMagicHere(List<Expression> expressions)
    {
        List<Expression> blockContents = new List<Expression>();
        var var = Expression.Variable(typeof(List<string>), "list");
        blockContents.Add(Expression.Assign(var,
            Expression.New(typeof(List<string>))));
        foreach (var expression in expressions)
        {
            if (expression.Type.IsArray)
            {
                blockContents.Add(Expression.Call(
                    var, var.Type.GetMethod("AddRange"), expression));
            }
            else
            {
                blockContents.Add(Expression.Call(var,
                    var.Type.GetMethod("Add"), expression));
            }
        }
        blockContents.Add(var); // last statement in a block is the effective
                                // value of the block
        return Expression.Block(new[] {var}, blockContents);
    }

}

Upvotes: 5

Related Questions