Ciel
Ciel

Reputation: 17762

Creating an IDictionary<string,Expression> proving difficult

I want to make a dictionary that has named LINQ Expressions, since I cannot store the expressions in my database. I will store the name of the expression that is needed, and a structured IList<T> where T is a parameter type that can be stored in the database.

But it isn't quite working how I want ... I've got the following layout. This isn't a real expression, I'm just trying to get something to compile for now..

public static class Evaluations {
        public static Dictionary<string, Expression> Expressions = new Dictionary<string, Expression> {
            {
                "First Expression",
                new Expression<Func<int, int, int>> =
                    (current, next) => (current + next)
            }
        };
    }

However I am being told ... well, pretty much nothing. The compiler errors make no sense. Simply stating

This is not a variable

How can I add a System.Linq.Expressions.Expression as the Value of my IDictionary<string, Expression> safely? Any suggestions?

Upvotes: 0

Views: 72

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502466

This isn't a valid expression:

new Expression<Func<int, int, int>> =
                (current, next) => (current + next)

What are you trying to assign a value to?

I think you mean:

(Expression<Func<int, int, int>>)((current, next) => current + next)

This compiles fine:

public static Dictionary<string, Expression> Expressions = 
    new Dictionary<string, Expression> {
    {
        "First Expression",
            (Expression<Func<int, int, int>>)((current, next) => current + next)
    }
};

Upvotes: 4

Related Questions