将Linq表达式作为字符串传递? [英] Passing a Linq expression as a string?
本文介绍了将Linq表达式作为字符串传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码可以正常工作
using (var ctx = new MyEntities())
{
var devices = ctx.Devices
.Where(x=> x.Device == "TEST")
.ToList();
return devices;
}
我想做的就是传递"Where"子句中的表达式.我看到它可以带一个字符串,但以下内容会引发错误:
What I would like to do is to pass in the expression that goes in the "Where" clause. I see that it can take a string but the following throws an error:
String expression = "x=> x.Device == \"TEST\"" ;
using (var ctx = new MyEntities())
{
var devices = ctx.Devices
.Where(expression)
.ToList();
return devices;
}
运行时错误消息为查询语法无效.近期术语>",第6行,第4列.";传递最初源自字符串的表达式的最佳方法是什么?
The error message at runtime is "The query syntax is not valid. Near term '>', line 6, column 4."; What would be the best way to pass in an expression that is initially derived from a string?
推荐答案
您必须手动构建表达式.
You have to build the Expression manually.
IIRC,LINQ101示例中有一个DynamicExpressions库可以为您完成此操作.
IIRC, there is a DynamicExpressions library in the LINQ101 samples that can do this for you.
这篇关于将Linq表达式作为字符串传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文