Welcome to the ASP.NET Core feedback portal. We’re happy you’re here! If you have feedback on how to improve the ASP.NET Core, we’d love to hear it!>
Thanks for joining our community and helping improve Syncfusion products!
The datagrid does not handle “contains” queries and filters against a database correctly using Entity Framework Core 3. I have an ASP.Net Core 3 project using the data grid in Razor Pages. If I set up the data to fetch from the backend DbContext, any searching or searching within excel style filters fails because contains cannot be mapped to an EF core query.
Pulling the data out into a List works, but obviously this is not good for performance reasons. This needs to work with a queryable from the DbContext. See below samples for a simple reproduction:
<ejs-gridid="Grid"allowPaging="true"allowFiltering="true" allowGrouping="true"allowSorting="true"toolbar="@(new List<string>() { "Search", })">
<e-grid-pagesettingspageSize="10"></e-grid-pagesettings>
<e-grid-editSettingsallowAdding="true"allowDeleting="true"allowEditing="true"mode="Dialog"></e-grid-editSettings>
<e-grid-filterSettingstype="Excel"></e-grid-filterSettings>
<e-data-managerurl="/SystemConfig/Test/DataSource"adaptor="UrlAdaptor"></e-data-manager>
<e-grid-columns>
<e-grid-columnfield="id"isPrimaryKey="true"allowEditing="false"visible="false"></e-grid-column>
<e-grid-columnfield="name"validationRules="@(new { required=true})"headerText="Name"type="string"></e-grid-column>
<e-grid-columnfield="description"headerText="Description"type="string"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public async Task<JsonResult> OnPostDataSource([FromBody]DataManagerRequest dm)
{
var glassTypesQuery = _context.GlassTypes.AsQueryable();
DataOperations operation = new DataOperations();
int count = glassTypesQuery.Count();
if (dm.Search != null && dm.Search.Count > 0) // Searching, throws error
{
glassTypesQuery = operation.PerformSearching(glassTypesQuery, dm.Search);
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
glassTypesQuery = operation.PerformSorting(glassTypesQuery, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering, throws error
{
glassTypesQuery = operation.PerformFiltering(glassTypesQuery, dm.Where, dm.Where[0].Condition);
}
//Paging
if (dm.Skip != 0)
{
glassTypesQuery = operation.PerformSkip(glassTypesQuery, dm.Skip);
}
if (dm.Take != 0)
{
glassTypesQuery = operation.PerformTake(glassTypesQuery, dm.Take);
}
var glassTypes = glassTypesQuery.ToList();
return dm.RequiresCounts ?
new JsonResult(new { result
= glassTypes, count = count }) : new JsonResult(glassTypes);
}
And here’s the error:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The LINQ expression 'Where<GlassType>(
source: Where<GlassType>(
source: DbSet<GlassType>,
predicate: (g) => Property<bool>(g, "Deleted") == False),
predicate: (g) => g.Id.ToString().ToLower().Contains("ann") | (g.Name ?? "Blanks").ToString().ToLower().Contains("ann") | (g.Description ?? "Blanks").ToString().ToLower().Contains("ann"))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
This happens with a search or when searching the excel style filters because they both pass a contains query to the backend:
The only workaround I have is to use System.Dynamic.Linq to construct my own "contains" query if the where operator is contains, something like this:
if (dm.Where[0].Operator == "contains")
{
query = query.Where(dm.Where[0].Field + ".Contains(@0)", dm.Where[0].value);
}
Not ideal or perfect in every scenario, it just makes searching the excel filters work.