执行未知类型的 DynamicExpression
如果有人非常熟悉 Linq.Dynamic 命名空间,我可以使用一些帮助 - 在互联网上找不到任何深入的资源。
基本上,我使用 DynamicExpression.ParseLambda 创建一个表达式,其中类型在编译时未知,
public Expression GetExpression(Type t, List<QueryFilter> filters)
{
// pseudo code
// extracts a string representation of the query as 'expressionString'
return DynamicExpression.ParseLambda(t, typeof(Boolean), expressionString, values);
}
其中 QueryFilter 是:
public class QueryFilter
{
string propertyName;
ExpressionType operationType;
object value;
}
它表示一个简单的二进制函数,例如“年龄 > 15”或其他函数。
这就是“GetExpression”函数的工作原理,它采用两种类型——一种是输入类型,另一种是输出类型,并最终生成通常使用 Func 委托创建的内容。它还需要一个表示查询的字符串和一个值的 params object[],分别是上面的“expressionString”和“values”。
但是,我在使用从 SqlMetal(.dbmc 文件)生成的 DataContext 执行 LINQ-to-SQL 中的动态表达式时遇到问题。
DatabaseContext db = new DatabaseContext(connectionString);
var filter = DynamicExpressionBuilder.
GetExpression(typeof(SysEventLogT), sysEventFilters)
var query = db.SysEventLogT.Where(filter);
产生以下错误,
System.Data.Linq.Table<DBReporting.Linq.Data.SysEventLogT>
不包含“Where”的定义,并且最佳扩展方法重载
System.Linq.Dynamic.DynamicQueryable.Where<T>(System.Linq.IQueryable<T>, string, params object[])
具有一些无效参数。
我知道我的 DataContext 实例实际上将 sql 表视为属性...我是否需要以某种方式用 GetProperty() 进行反映才能使其正常工作?或者也许我需要创建另一个 .Where 扩展名?
If anyone is very familar with the Linq.Dynamic namespace I could use some help -- couldn't find any indepth resources on the internet.
Basically I'm using DynamicExpression.ParseLambda to create an expression where the type is not known at compile time,
public Expression GetExpression(Type t, List<QueryFilter> filters)
{
// pseudo code
// extracts a string representation of the query as 'expressionString'
return DynamicExpression.ParseLambda(t, typeof(Boolean), expressionString, values);
}
Where a QueryFilter is:
public class QueryFilter
{
string propertyName;
ExpressionType operationType;
object value;
}
Which represents a simple binary function like "Age > 15" or something.
This is how the 'GetExpression' function works, it takes 2 types -- one that is the input type and one that is the output type, and ultimately generates what would normally be created with a Func delegate. It also takes a string that represents the query and a params object[] of values, which are 'expressionString' and 'values' above, respectively.
However I am having trouble executing the dynamic expression in LINQ-to-SQL, using a DataContext generated from SqlMetal (.dbmc file).
DatabaseContext db = new DatabaseContext(connectionString);
var filter = DynamicExpressionBuilder.
GetExpression(typeof(SysEventLogT), sysEventFilters)
var query = db.SysEventLogT.Where(filter);
Produces the following error,
System.Data.Linq.Table<DBReporting.Linq.Data.SysEventLogT>
does not contain a definition for 'Where' and the best extension method overload
System.Linq.Dynamic.DynamicQueryable.Where<T>(System.Linq.IQueryable<T>, string, params object[])
has some invalid arguments.
I know that my DataContext instance actually treats the sql tables as properties...do I need to reflect with GetProperty() somehow for this to work? Or perhaps I need to create another .Where extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 GetExpression 返回一个表达式类型 - DynamicQueryable.Where 方法在用作扩展方法时,需要一个字符串作为第一个参数。
您需要调用Where,看起来像这样:
此外,您可以尝试以下操作,只是为了明确:
请注意,如果更容易,您可以构建一个包含完整过滤器的字符串,而根本不使用params object[]参数:
Your GetExpression is returning an Expression type - the DynamicQueryable.Where method, when used as an extension method, expects a string as the first parameter.
You need your call to Where to look like this:
Also, you could try the following, just to be explicit:
Note that if easier, you can build a sting containing the full filter and not use the params object[] parameter at all: