0

Trying to create the following expression using expression trees (would like help)

List<string> lstName = dt_Name.Select(y => y.Name);

List<string> lstLabName = dt_Label.Select(x => x.LabelName).Where(p => p.LabelName.StartsWith(lstName + '_'));

I would like it to actually go over all of lstName and find all the instances in lstLabName that meet the condition.

The code I came up with so far is:

    private BinaryExpression CreateBinaryExpression(string buildName)
    {
        // x.LabelName
        var param = Expression.Parameter(typeof(dt_Label), "x");
        var key = param.GetType().GetProperty("LabelName");
        var left = Expression.MakeMemberAccess(param, key);

        //ParameterExpression left = Expression.Parameter(typeof(string), "p"); //p
        ConstantExpression constantExpression = Expression.Constant(buildName, typeof(string));

        // x.LabelName == buildName
        return Expression.MakeBinary(ExpressionType.Equal, left, constantExpression);
    }

    private BinaryExpression CalculateLambdaExp(List<string> lstBuildName) 
    {
            BinaryExpression binaryExpression = CreateBinaryExpression(lstBuildName[0]);

            if (lstBuildName.Count() > 1)
            {
                List<string> lstnStr = lstBuildName;
                lstnStr.RemoveAt(0);
                BinaryExpression calculatedLambdaExp = CalculateLambdaExp(lstnStr);
                binaryExpression = Expression.MakeBinary(ExpressionType.AndAlso, binaryExpression, calculatedLambdaExp);
            }

            return binaryExpression;
    }

    private List<string> RunLambdaExpression(List<string> BuildName)
    {
        ParameterExpression left = Expression.Parameter(typeof(string), "p"); //p

        var factorial = Expression.Lambda<Func<List<string>, List<string>>>(CalculateLambdaExp(BuildName), left);
        var n = factorial.Compile()(BuildName);
        List<string> lst = n.ToList();

        return lst;
    }

I'm getting plenty of runtime errors. would appreciate any help.

figured most of it out:

changed the function CreateBinaryExpression to:

    private Expression CreateBinaryExpression(string buildName)
    {
        // x.LabelName
        var param = Expression.Parameter(typeof(dt_Label), "x");
        var key = typeof(dt_Label).GetProperty("LabelName");

        var left = Expression.MakeMemberAccess(param, key);

        ConstantExpression constantExpression = Expression.Constant(buildName + '_', typeof(string));

        //x.LabelName.startsWith(buildName_)
        MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
        Expression call = Expression.Call(left, mi, constantExpression);

        return call;
    };

and changed all references to the function to receive Expression instead of BinaryExpression

5
  • Without looking further, I can see that param.GetType().GetProperty("LabelName"); is wrong - you probably want typeof(dt_label) here. Commented Apr 11, 2013 at 17:46
  • 1
    Why do you need expression trees? Commented Apr 11, 2013 at 18:41
  • There doesn't appear to be a question in this question. What's your question? Commented Apr 11, 2013 at 18:54
  • my question is how to build the expression: List<string> lstLabName = dt_Label.Select(x => x.LabelName).Where(p => p.LabelName.StartsWith(lstName + '_')); i need expression tree because "lstName" is a list of strings and i want the expression to iterate in it. Commented Apr 11, 2013 at 19:04
  • my run time error fails on CreateBinaryExpression function Commented Apr 11, 2013 at 19:07

1 Answer 1

3

This line is a problem

var key = param.GetType().GetProperty("LabelName");

param.GetType() is going to return System.Linq.Expressions.ParameterExpression which does not have a LabelName property.

You should use typeof(dt_Label).GetProperty("LabelName")

Sign up to request clarification or add additional context in comments.

6 Comments

instead of: var key = param.GetType().GetProperty("LabelName"); i tried: var key = param.Type.GetField("LabelName"); but key gets null, why?
and what i'm trying to get is: dt_Label.LabelName
Then try typeof(dt_Label).GetProperty("LabelName"). I could be wrong about param.Type
thankyou so much it worked with: var key = typeof(dt_Label).GetProperty("LabelName"); write a new answer with your comment and i'll write that that's the answer so far
how can i do startswith? as it: Expression.StartsWith
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.