Telerik MVC Grid: Create a custom FilterDescriptor based on the lambda expression

advertisements

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 ?


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) { }