c# - LINQ: How to force a value based reference? -


i want provide set of filters user pick from, , each filter correspond expression<func<x, bool>>. so, might want take dynamic list of available items ('joe', 'steve', 'pete', etc), , create collection of "hard-coded" filters based on names, , let user select filter(s) wants use. problem when try "hard-code" expression based on string value dynamic list, expression still storing value as, looks me, property hanging off of anonymous type (and don't know how serialize anon. type). sorry if confusing, i'm not quite sure how articulate this.

here's sample code:

    public class foo     {         public string name { get; set; }     }     static void main(string[] args)     {         foo[] source = new foo[]             {                 new foo() { name = "steven" } ,                 new foo() { name = "john" } ,                 new foo() { name = "pete" },             };              list<expression<func<foo, bool>>> filterlst = new list<expression<func<foo, bool>>>();             foreach (foo f in source)             {                 expression<func<foo, bool>> exp = x => x.name == f.name;                 filterlst.add(exp);             }     } } 

my problem when @ when @ body of expression, reads follows:

(x.name = value(consoleapplication1.program+<>c__displayclass3).value) 

when want first 1 read this:

(x.name = "steven") 

(if change code this, instead, that's get:

      expression<func<foo, bool>> exp = x => x.name == "steven"; 

)

i've tried forcing value local string value before sticking expression, doesn't seem help:

    list<expression<func<foo, bool>>> filterlst = new list<expression<func<foo, bool>>>();     foreach (foo f in source)     {         string value = f.name;         expression<func<foo, bool>> exp = x => x.name == value;         filterlst.add(exp);     } 

i don't understand why (or how) it's still looking @ anonymous type once i'm using local variable declared string. there way make work want to?

the anon-type compiler-generated type using perform capture of variables. delegates can hack around implementing capture hand, not lambda expressions compiled expression-trees.

two choices:

  • build expression tree explicitely on code via expression.constant etc
  • learn how handle anon types

the latter isn't bad actually; memberexpression typically, although have code kicking around covers in full detail. can provide examples of buildin expression tree, i'm not @ pc @ moment , doesn't lend ipod typing...

from brief read of question i'd @ first option more second.

oh, , watch out; first foreach code in question looks susceptible notorious l-value capture issue ;)


edit: found pc ;p

        var param = expression.parameter(typeof(foo), "x");         var body = expression.equal(             expression.propertyorfield(param, "name"),             expression.constant(f.name, typeof(string)));          var exp = expression.lambda<func<foo, bool>>(body, param);         filterlst.add(exp); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -