filter Datagrid with an expresion builder

Hi Syncfusion.

I run a SFDatagrid in a WPF that is attached to an observable collection (_ws_Data) based of a object model (ws_Result). I use the standard filtering as an example below.

public bool FilterRecords(object o)

{

string filterText = WS_search.Text.ToLower();
var item = o as ws_Result;

return (item.ws_name.ToLower().Contains(filterText) || item.ws_sn.ToLower().Contains(filterText));

}

This is a condensed version of the entire filter, but you get the idea. What I am wanting to achieve is to apply Dynamic LINQ to the return. An example of what I want to achieve is something along the lines of a Dynamic LINQ query, or an SQL like query. As the query will be a string, I am having difficulties converting that to a filter as it needs to convert to a Bool. Currently I am unable to apply a Dynamic LINQ as all examples I have seen uses where clause, and as my item is an object, I cannot apply it.

Along the lines of:

return ("ws_name LIKE 1889 OR ws_sn LIKE 1889") or return item.where ("ws_name LIKE 1889 OR ws_sn LIKE 1889")

Reason for this is I want to build a Query based of a whole heap of combo box's where the end user can create their own query. This query will be a string based of results like the one above and will be stored for future reference in a Json file. When I was using data tables, I was able to do the above as I used SQL like strings to apply filters and set the item source. This was slow and cumbersome, and the new way is faster, but would still like that customizable option


1 Reply

MS Malini Selvarasu Syncfusion Team June 3, 2025 02:04 PM UTC

Hi Peter,

Based on the information provided, your requirement is to apply dynamic filtering of records in the SfDataGrid using Dynamic LINQ queries. To achieve this, we constructed a query string dynamically and used it to filter the collection bound to the grid. Please refer to the code snippet below for implementation details.

Code snippet to dynamically build a query string and filter the data bound to the grid.
public void ApplyFilter()
{
    string query = BuildDynamicQuery(Filters.ToList());
    var filtered = Items.AsQueryable().Where(query).ToList();
    FilteredItems.Clear();

    foreach (var item in filtered)
          FilteredItems.Add(item);
}
 
private string BuildDynamicQuery(List<FilterCondition> conditions)
{
    var expressions = new List<string>();
    foreach (var cond in conditions)
    {
        string val = cond.Value.Replace("\"", "\\\"");
        string expr = null;
        if (cond.Operator == "Contains")
            expr = $"{cond.PropertyName}.Contains(\"{val}\")";
        else if (cond.Operator == "Equals")
            expr = $"{cond.PropertyName} == \"{val}\"";
        else if (cond.Operator == "StartsWith")
            expr = $"{cond.PropertyName}.StartsWith(\"{val}\")";
        else if (cond.Operator == "EndsWith")
            expr = $"{cond.PropertyName}.EndsWith(\"{val}\")";
        if (!string.IsNullOrWhiteSpace(expr))
            expressions.Add(expr);
    }
    return string.Join(" AND ", expressions);
}
This approach dynamically constructs the filter expression based on user-defined conditions and applies it to the data collection using Dynamic LINQ.
Please find the sample demo attached for your reference.
Kindly let us know if this solution meets your requirements or if you need further assistance.
Regards,
Malini Selvarasu

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDataGrid_Demo_8487f1e6.zip


Loader.
Up arrow icon