c# - Convert new object creation to linq expression for linq to entities -


i doing query using linq entities , want reuse same code different select statement. query:

return f in getfields<t>()      f.id == id      select new prop<v>          {            name = f.name,            value = tovalue(f.value)          }; 

where tovalue func creates new value object. want use several tovalue function have kind of body:

var tovalue = f => new derivedvalueclass {                  firstfield = f.firstfield,                  secondfield = f.secondfield               } 

so simple assignment, translated sql entities. when execute code, linq entities states:

the linq expression node type 'invoke' not supported in linq entities 

i guess not possible call tovalue function, since can't translated. how can create expression function tovalue in order use inside linq query?

eventually find out linqkit allows kind of things easily:

expression<func<t, v>> tovalue =  => new derivedobject() {      // perform assignment here }  return f in getfields<t>().asexpandable() // <- linqkit asexpandable() extension method  f.id == id  select new prop<v>      {        name = f.name,        value = tovalue.invoke(f.value) // <- linqkit invoke() extension method      }; 

it important tovalue expression, since compiler create expressiontree instead of generating lambda expression. linqkit use trick in order replace tovalue invocation expression tree. more info on linqkit site , here


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -