动态表达式 API:我可以做谓词,如何编写 OrderBy 规范?
我想出了如何根据客户端提供的字符串进行谓词 Dynamic Linq (这被包装在规范对象中):
return System.Linq.Dynamic.DynamicExpression.ParseLambda<TE, bool>
(filter.ToString(), arguments.ToArray())
其中过滤器是表达式语言的字符串,如其帮助中所述 文件。就像魅力一样。
但是,是否可以将 orderby 字段的字符串列表转换为强类型表达式,如下所示:
Expression<Func<E, object>> orderby
注意:
这是我的存储库方法,用于过滤和排序项目,
public IList<E> Get(Expression<Func<E, bool>> filterLambda = null,
Expression<Func<E, object>> orderbyLambda = null,
int? page = null,
int? pageSize = null)
我想使用以下方式调用它:
var a = Repo.Get( filterLambda: Specification.Where( StringListOfFilters),
orderbyLambda: Specification.OrderBy( StringListOfOrderBy),
page: 1,
pageSize: 100 );
有关的任何想法如何将 OrderBy 字段的字符串列表转换为强类型 lambda 表达式?字符串的示例如下:
fieldname
fieldname descending
另请注意:
我正在翻译来自客户端的字符串,这些字符串是使用正则表达式验证它们以防止注入的 fieldname op value
。另外,我仅在规范对象中引用 Dynamic Linq,而不是存储库。
I figured out how to do a predicate from a string supplied by a client based on Dynamic Linq (this is wrapped in a Specification object):
return System.Linq.Dynamic.DynamicExpression.ParseLambda<TE, bool>
(filter.ToString(), arguments.ToArray())
where filter is a string of expression language as described in their help file. Works like a charm.
However, is it possible to translate a string list of orderby fields into a strongly typed expression like this:
Expression<Func<E, object>> orderby
Notes:
Here's my repository method to filter and order items
public IList<E> Get(Expression<Func<E, bool>> filterLambda = null,
Expression<Func<E, object>> orderbyLambda = null,
int? page = null,
int? pageSize = null)
I would like to call it using:
var a = Repo.Get( filterLambda: Specification.Where( StringListOfFilters),
orderbyLambda: Specification.OrderBy( StringListOfOrderBy),
page: 1,
pageSize: 100 );
Any ideas on how to convert a string list of OrderBy fields into a strongly typed lambda expression? Examples of the strings would be:
fieldname
fieldname descending
Another note:
I am translating the strings coming from the client, which are fieldname op value
validating them with Regex to prevent injection. Also, I am only referencing Dynamic Linq in the Specification object, not the repository.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不再是问题了..但是看起来您的解决方案已在此处给出。
This is probably not a problem anymore.. however it looks like your solution is given here.