none
EF Dynamic Query using Expression Tree issues: variable "p" referenced from scope, but is not defined RRS feed

  • Question

  • I'm trying to learn Expression trees...somewhat arduous, somwhat frustrating, but... very interesting.

    I'm getting this error when running the code below:

    variable 'p' of type 'PV0001504_0003392.Model.t_pick_detail' referenced from scope '', but it is not defined

    Can anyone spot what I'm doing wrong?  The failing line of code is here:  var finalcompile = final.Compile();

    To explain the code, I took a good working Lambda deconstructed the tree, to learn, then attempted to inject new stuff so I could dynmaically create EF queries.  I'm trying to get to the last lines of code to do this:

    var stuff = ad.t_pick_detail.Where(finalcode);
                int rows = stuff.Count();

     public void Test()
            {
                Model.AADEntities ad = new Model.AADEntities();
                ad.t_pick_detail.Where(p => p.status == "picked");
                q.Expression<Func<Model.t_pick_detailbool>> func = (p => p.item_number == "05");
     
                // q.TypeBinaryExpression tbin = q.Expression.TypeIs(ParameterExpression, typeof(Model.t_pick_detail));
     
                TPickDetailProperties();
     
                //find the appropriate typed field name for t_pick_detail.
                string fieldname = "status";
                var TheField = typeof(Model.t_pick_detail).GetProperty(fieldname);
     
                //create a dummy type parameter of t_pick_detail (entity class)
                q.Expression ParameterExpression = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");
                string thevalue = "TheStringValue";
                //this will be the right side.
                q.ConstantExpression EqualToThis = q.Expression.Constant(thevalue);
                //now join the type parameter to the field.  p.status, the node type will be MEMBERACCESS
                //this should now be the left side...
                q.MemberExpression ThisMember = q.Expression.Property(ParameterExpression, TheField);
                //notice that bod and bin both cast to their prospective types
                q.Expression bod = func.Body;
                //but we want the BinaryExpression methods to materialize...
                q.BinaryExpression bin = (q.BinaryExpression)bod;
                q.MemberExpression left = (q.MemberExpression)bin.Left;
                MemberInfo leftmi = left.Member;
                q.Expression leftexpression = left.Expression;
     
                q.Expression right = bin.Right;
                MethodInfo mi = bin.Method;
     
                //let's try the update method to change left and right values to binaryexpression.
     
                string name = func.Name;
                q.Expression newbin = bin.Update(ThisMember, null, EqualToThis);
                q.Expression<Func<Model.t_pick_detailbool>> func2;
                var parms = func.Parameters;
                q.ParameterExpression finalparms = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");
                q.LambdaExpression final = q.Expression.Lambda<Func<Model.t_pick_detailbool>>(newbin, finalparms);
                q.ExpressionType t = func.NodeType;
                var finalcompile = final.Compile();
                var CompiledCode = func.Compile();
                var stuff = ad.t_pick_detail.Where(CompiledCode);
                int rows = stuff.Count();
            }

    JP Cowboy Coders Unite!

    Wednesday, February 15, 2012 3:39 PM

Answers

  • Well I found the answer to this question:  This message "referenced from scope, but is not defined" has many meanings but the one that resolved this issue was that this line:

    q.Expression ParameterExpression = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");

    and this line, conflicted... Yes that's right!!!!!

     q.ParameterExpression finalparms = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");

    If you look at the code above you'll notice two Expressions that use the same string to name the variable even though one is in an expression named ParameterExpression and the other is in the finalparms expression!!!!!!

    Now how's that for ridiculous error messaging?  Lost at least a week on this one!!!!  Thanks MSFT...  but I did learn more about Expression (arghhhh!) trees.

    MSFT... How come you guys can't tell us lamers out here what the real problem is?  I guess that's how working class heros are made, but sheesh, nobody gets paid for intrepreting messages right?


    JP Cowboy Coders Unite!


    Friday, February 17, 2012 1:49 PM

All replies

  • Hi,

    To run dynamic query, you can see here: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx, i think it is helpful.


    I am fish.

    Thursday, February 16, 2012 8:41 AM
  • I've seen that post, which is dated to 4 years ago.  Question, is the dynamic LINQ dll in .NET 4.0?  Or is this a standalone unsupported extension?  I'm not all that interested in unsupported code, but will take another look because I do like the string binding concept (late binding)...  In Gu's post I was also confused because I'm using EF and not LINQ to SQL (now dead).  I didn't know if I was going down a dead path or not.

    Still, the answer to original question has not been answered. 

    Thanks!


    JP Cowboy Coders Unite!

    Thursday, February 16, 2012 1:35 PM
  • Well I found the answer to this question:  This message "referenced from scope, but is not defined" has many meanings but the one that resolved this issue was that this line:

    q.Expression ParameterExpression = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");

    and this line, conflicted... Yes that's right!!!!!

     q.ParameterExpression finalparms = q.Expression.Parameter(typeof(Model.t_pick_detail), "p");

    If you look at the code above you'll notice two Expressions that use the same string to name the variable even though one is in an expression named ParameterExpression and the other is in the finalparms expression!!!!!!

    Now how's that for ridiculous error messaging?  Lost at least a week on this one!!!!  Thanks MSFT...  but I did learn more about Expression (arghhhh!) trees.

    MSFT... How come you guys can't tell us lamers out here what the real problem is?  I guess that's how working class heros are made, but sheesh, nobody gets paid for intrepreting messages right?


    JP Cowboy Coders Unite!


    Friday, February 17, 2012 1:49 PM