tags:

views:

639

answers:

2

I want to use Linq expression for some dynamic features. I need And, Or and Not expressions.. I couldn't get much..

We want to check whether certain feature has been enabled or not in our system and based on that we will decide whether to show the menu item or not. we have formed rules in XML format, I know to convert the rule to AST but I don't know to map to Linq expression.

Rules Are like : Feature1Enabled And Feature2Eenabled or (Feature3Disabled And Not Feature5Enabled)

Here "Feature1Enabled", "Feature2Eenabled" etc are name of the feature. We will pass this string to IsFeatureEnabled function to check whether a feature has been enabled or not.

  public delegate bool IsEnabledDelegate(string name, string value);

    public static bool IsFeatureEnabled(string name, string value)
    {
        if (name == "MV")
            return true;

        if (name == "GAS" && value == "G")
            return true;
        //More conditions goes here
        return false;
    }

    static void Main(string[] args)
    {
        Expression<Func<string, string, bool>> featureEnabledExpTree = 
                      (name, value) => IsFeatureEnabled(name, value);

        //I want the equal of the following statement in C# Linq Expression

       bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F")

    }

I want the equivalent to the bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F")

in Linq expression Format.. However I can convert them to dynamically based on my AST notations..

Thank you so much.. If you need more info, tell me in comments..

+1  A: 

like that?

Expression<Func<bool>> featureEnabledExpTree = () =>
    IsFeatureEnabled("MV", "") &&
    IsFeatureEnabled("GAS", "G") ||
    !IsFEatureEnabled("GAS", "F");
Thomas Danecker
"However I can convert them to dynamically based on my AST notations" - which I'm interpreting to mean that it has to be built based on data, so the compiler's ability to translate C# to expression trees might not be useful.
Marc Gravell
+3  A: 
    ParameterExpression name = Expression.Parameter(typeof(string), "name"),
        value = Expression.Parameter(typeof(string), "value");

    // build in reverse
    Expression body = Expression.Constant(false);
    body = Expression.Condition(
        Expression.AndAlso(
            Expression.Equal(name, Expression.Constant("GAS")),
            Expression.Equal(value, Expression.Constant("G"))
        ), Expression.Constant(true), body);
    body = Expression.Condition(
        Expression.Equal(name, Expression.Constant("MV")),
        Expression.Constant(true), body);

    Expression<Func<string, string, bool>> featureEnabledExpTree =
        Expression.Lambda<Func<string, string, bool>>(body, name, value);


    // test in isolation
    var featureEnabledFunc = featureEnabledExpTree.Compile();
    bool isMatch1 = featureEnabledFunc("MV", "")
        && featureEnabledFunc("GAS", "G") || !featureEnabledFunc("GAS", "F");


And then, if you need the second portion as an expression tree too:

    //I want the equal of the following statement in C# Linq Expression
    Expression<Func<bool>> test =
        Expression.Lambda<Func<bool>>(
            Expression.OrElse(
                Expression.AndAlso(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("MV"),
                        Expression.Constant("")
                    ),
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("G")
                    )
                ),
                Expression.Not(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("F")
                    )
                )
            )
        );

    bool isMatch = test.Compile()();
Marc Gravell
Hi Marc, Thank you so much. Yes, I need to build the expression at runtime.. If possible, please give me some more details..
Gopalakrishnan Subramani
I don't need to have if statement.. or else portion to evaluated. I need only expression
Gopalakrishnan Subramani
Thomas Danecker
Thanks Marc. You did well again..
Gopalakrishnan Subramani