Telerik MVC Grid:基于 lambda 表达式创建自定义 FilterDescriptor

发布于 2024-11-25 08:56:55 字数 631 浏览 1 评论 0原文

目前我正在使用此代码创建自定义过滤器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜血染红嫁衣 2024-12-02 08:56:56

在 Telerik 的 QueryableExtensions.cs 文件中,此扩展方法将完成这项工作:

/// <summary>
/// Filters a sequence of values based on a collection of <see cref="IFilterDescriptor"/>.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="filterDescriptors">The filter descriptors.</param>
/// <returns>
/// An <see cref="IQueryable" /> that contains elements from the input sequence
/// that satisfy the conditions specified by each filter descriptor in <paramref name="filterDescriptors" />.
/// </returns>
public static IQueryable Where(this IQueryable source, IEnumerable<IFilterDescriptor> filterDescriptors) { }

In the QueryableExtensions.cs file from Telerik, this extension method will do the job:

/// <summary>
/// Filters a sequence of values based on a collection of <see cref="IFilterDescriptor"/>.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="filterDescriptors">The filter descriptors.</param>
/// <returns>
/// An <see cref="IQueryable" /> that contains elements from the input sequence
/// that satisfy the conditions specified by each filter descriptor in <paramref name="filterDescriptors" />.
/// </returns>
public static IQueryable Where(this IQueryable source, IEnumerable<IFilterDescriptor> filterDescriptors) { }
负佳期 2024-12-02 08:56:56

好吧,你目前的做法并没有太大问题。除非您确定添加为 linq 表达式,否则请使用以下示例。

var query = from t in query where t.Artist.Contains(Artist) && t.Name.Contains(Name) select t;

如果 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.

var query = from t in query where t.Artist.Contains(Artist) && t.Name.Contains(Name) select t;

If both Artist and Name are empty or null then it will return all anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文