[英]LINQ Dynamic Assignment Expressions
I have the following code that I would rather have defined dynamically. 我有以下代码,我宁愿动态定义。
var candidates = People.Where(x=> (x.Age >18 && x.Age < 25)) .AsQueryable().Select( c=> { c.Category = "F190 MISSING" ; return c; });
What I would however want is to have the above assignment defined as a string to be executed at runtime like so 但是我想要的是将上述分配定义为要在运行时执行的字符串,如下所示
String Conditional= "c=> { c.Category = @0; return c; }";
String[] ExpressionParameters = new [] {"F190 MISSING"};
var candidates = People.Where(x=> (x.Age >18 && x.Age < 25)).AsQueryable().Select( Conditional,ExpressionParameters);
So far, with my attempts, I have been getting either a Expression expected error message or a NO PROPERTY C EXISTS IN PERSON . 到目前为止,通过我的尝试,我收到了Expression预期错误消息或PERSONTY C EXISTS IN PERSON 。 How can I better define my assignment expression dynamically?
如何更好地动态定义我的赋值表达式? Thanks in advance.
提前致谢。
The first one does not work, because c# compiler is not able to transform multi-line lambda into Expression Tree (and IQueryable.Select
expects expression). 第一个不起作用,因为c#编译器无法将多行lambda转换为Expression Tree(并且
IQueryable.Select
需要表达式)。
The C# and Visual Basic compilers can generate expression trees only from expression lambdas (or single-line lambdas).
C#和Visual Basic编译器只能从表达式lambda(或单行lambda)生成表达式树。 It cannot parse statement lambdas (or multi-line lambdas).
它无法解析语句lambda(或多行lambda)。 For more information about lambda expressions in C#, see Lambda Expressions (C# Programming Guide) ;
有关C#中的lambda表达式的更多信息,请参见Lambda Expressions(《 C#编程指南》) ; (...).
(...)。
The second one can be done using libraries like DynamicLINQ. 第二个可以使用DynamicLINQ之类的库来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.