IQueryable.Where does not accept LoadDataArgs.Filter

In my WebAssembly project I have bound a DataGrid do an IList which has been working fine, but I would prefer to use virtualization instead of paging. The example datagrid-virtualization-loaddata looks straight forward, but for me it does not compile. Is it because I don't have the data in a DbContext? How could I resolve that?

public IList<TItem> Rows { get; private set; }
public async void LoadData(LoadDataArgs args)
{
		var query = Rows.AsQueryable();
		if (!string.IsNullOrEmpty(args.Filter))
		{
			query = query.Where(args.Filter);
		}

|Error|CS1503|Argument 2: cannot convert from 'string' to 'System.Func<TItem, bool>'

You need @using System.Linq.Dynamic.Core

1 Like

Thanks! Quite simple when you know it. Just a follow-up question: Does in-line editing work with virtualization?

Btw, the example code in datagrid-virtualization-loaddata says

        orderDetails = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
        count = dbContext.OrderDetails.Count();

But it would be nicer to have count = query.Count();

I know the example has this but I really wish it was emphasized. Really easy to miss...

In my case, despite using System.Linq.Dynamic.Core
The Filter parameter of the LoadDataArgs class did not contain a string format compatible with IQueryable for Linq:

contains(Summary, 'Balmy')

and I received the Exception: Character literal must contain exactly one character

So I had to use Radzen.Query object of the grid itself, giving me a compatible format:

(Summary == null ? "" : Summary).Contains("balm")

private ODataEnumerable<WeatherForecast> forecasts;
RadzenDataGrid<WeatherForecast> grid;
int count;
bool isLoading = false;

async Task LoadData(LoadDataArgs args)
{
    isLoading = true;
    var result = await WeatherApi.GetWeatherAsync(grid.Query);
    forecasts = result.Value.AsODataEnumerable();
    count = result.Count;
    isLoading = false;
}

You are using ODataEnumerable and RadzenDataGrid creates OData compatible filter expressions.

Did you find any solution for this?

Yes, I have created a POCO class:

    public class DataServiceResult<T>
    {
        public int Count { get; set; }
        public IEnumerable<T>? Value { get; set; }
    }

And using it on the ApiClient:

    public async Task<DataServiceResult<WeatherForecast>> GetWeatherAsync(Radzen.LoadDataArgs args, CancellationToken cancellationToken = default)
    {
        JsonContent content = JsonContent.Create(args);

        var response = await httpClient.PostAsync("/weatherforecast", content, cancellationToken);

        return await response.ReadAsync<DataServiceResult<WeatherForecast>>();
    }

And in the razor view:

@code {
    private IEnumerable<WeatherForecast>? forecasts;

    int count;
    bool isLoading = false;

    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;

        var result = await WeatherApi.GetWeatherAsync(args);

        forecasts = result.Value;
        count = result.Count;

        isLoading = false;
    }
}

And the Get method in the ApiService:

        public DataServiceResult<WeatherForecast> Get(Radzen.LoadDataArgs args)
        {
            forecast ??= Enumerable.Range(1, 500).Select(index =>
                    new WeatherForecast
                    (
                        DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                        Random.Shared.Next(-20, 55),
                        summaries[Random.Shared.Next(summaries.Length)]
                    )).ToList();

            var query = forecast.AsQueryable();

            if (!string.IsNullOrEmpty(args.Filter))
            {
                query = query.Where(args.Filter);
            }

            if (!string.IsNullOrEmpty(args.OrderBy))
            {
                query = query.OrderBy(args.OrderBy);
            }

            int total = query.Count();

            if (args.Skip.HasValue)
            {
                query = query.Skip(args.Skip.Value);
            }
            if (args.Top.HasValue)
            {
                query = query.Take(args.Top.Value);
            }

            var rows = query.ToList();

            var result = new DataServiceResult<WeatherForecast>
            {
                Value = rows,
                Count = total
            };

            return result;
        }

Regards,