为“like”创建表达式树在小数字段上
我想为查询表达式创建一个表达式树,如下所示: 员工=> employee.Salary.StartsWith("28")
这样sql就可以显示为: 其中 (employee.salary like '28%')
问题在于,employee 对象的属性 Salary 是小数,而 StartsWith 不是小数的属性。我怎样才能抽出时间来做这件事。
我的错误表达式树语法如下:
var searchTextExp = Expression.Constant("28");
var parameterExp = Expression.Parameter(typeof(EmployeeEntity), "employee");
var propertyExp = Expression.Property(parameterExp, "Salary");
var startsWithExp = Expression.Call(propertyExp, "StartsWith", null,
searchTextExp);
Expression<Func<EmployeeEntity, bool>> searchExpr =
Expression.Lambda<Func<EmployeeEntity, bool>>
(startsWithExp, new ParameterExpression[] { parameterExp });
I would like to create an expression tree for a query expression that looks something like this:
employee => employee.Salary.StartsWith("28")
So that the sql could appear as:
where (employee.salary like '28%')
The problem is that the property Salary of the employee object is a decimal and StartsWith is not a property of a decimal. How can i get around to do this.
My erroneous expression tree syntax is as follows:
var searchTextExp = Expression.Constant("28");
var parameterExp = Expression.Parameter(typeof(EmployeeEntity), "employee");
var propertyExp = Expression.Property(parameterExp, "Salary");
var startsWithExp = Expression.Call(propertyExp, "StartsWith", null,
searchTextExp);
Expression<Func<EmployeeEntity, bool>> searchExpr =
Expression.Lambda<Func<EmployeeEntity, bool>>
(startsWithExp, new ParameterExpression[] { parameterExp });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您知道最简单的解决方案(可能也适用于 Linq to Sql)是:
You know the easiest solution (that will probably also work in Linq to Sql) is:
我已经设法使用函数映射来解决这个问题,这是 LLBLGEN Pro 的一个功能。
然后,我将 FunctionMappings 的实例附加到 LINQ 元数据:
然后按如下方式使用该函数:
I have managed to solve this using Function Mappings which is a feature of LLBLGEN Pro.
I then attached an instance of FunctionMappings to the LINQ Metadata:
Then used the Function as follows:
如果 llblgen 不支持 ToString(),您可以通过创建执行强制转换的视图来解决该问题。你的目标数据库是什么?
如果是 SQL Server,您可以使用 CAST() 函数来创建视图。
例如
,之后您应该能够将该视图映射到一个新类并执行如上所述的查询。
If llblgen does not support ToString() you can workaround the problem by creating a View that does the cast. What's your target database?
If it's SQL Server you can use the CAST() function to create the view.
e.g.
After that you should be able to map that view against a new class and do the query like stated above.