Telerik MVC Grid:基于 lambda 表达式创建自定义 FilterDescriptor
目前我正在使用此代码创建自定义过滤器:
var fName = new FilterDescriptor
{
Member = "Name",
MemberType = typeof(string),
Operator = FilterOperator.Contains,
Value = name
};
它将像这样添加到 GridCommand 中:
gridCommand.FilterDescriptors.Add(fName);
但是,想要基于 Linq lambda 表达式创建过滤器,例如:
IQueryable<CD> query = ...
if (!string.IsNullOrWhiteSpace(Artist))
{
query = query.Where(cd => cd.Artist.Contains(Artist));
}
if (!string.IsNullOrWhiteSpace(Name))
{
query = query.Where(cd => cd.Name.Contains(Name));
}
如何执行此操作?
Currently I'm using this code to create a custom filter:
var fName = new FilterDescriptor
{
Member = "Name",
MemberType = typeof(string),
Operator = FilterOperator.Contains,
Value = name
};
Which will be added to the GridCommand like this:
gridCommand.FilterDescriptors.Add(fName);
However, would like to create filters based on Linq lambda expression like:
IQueryable<CD> query = ...
if (!string.IsNullOrWhiteSpace(Artist))
{
query = query.Where(cd => cd.Artist.Contains(Artist));
}
if (!string.IsNullOrWhiteSpace(Name))
{
query = query.Where(cd => cd.Name.Contains(Name));
}
How to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Telerik 的 QueryableExtensions.cs 文件中,此扩展方法将完成这项工作:
In the QueryableExtensions.cs file from Telerik, this extension method will do the job:
好吧,你目前的做法并没有太大问题。除非您确定添加为 linq 表达式,否则请使用以下示例。
如果 Artist 和 Name 都为空或 null,那么无论如何它都会返回全部。
Ok so there isnt much wrong with the way you are doing it currently. Except if you were determined to add as linq expression then use the following example.
If both Artist and Name are empty or null then it will return all anyway.