Translation of method 'bool.ToString' failed when searching a grid that contains bool field

Hello,

I am getting the following error when I use the search box in a grid in a Blazor application using 18.3.0.48 controls when the grid has a boolean field.

    System.InvalidOperationException
  HResult=0x80131509
  Message=The LINQ expression 'DbSet<Batches>()
    .Where(b => b.BatchID.ToString().ToLower().Contains("a") | b.IsMade.ToString().ToLower().Contains("a"))' could not be translated. Additional information: Translation of method 'bool.ToString' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  Source=Microsoft.EntityFrameworkCore


The error is thrown from this line in the CustomAdaptor:

        int count = DataSource.Cast<Batches>().Count();




The grid contains just an integer ID and boolean "IsMade" field.

@page "/test"

<SfGrid TValue="Batches" ID="Grid" AllowSorting="true" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Search", "Update", "Cancel" })">
    <SfDataManager Adaptor="Adaptors.CustomAdaptor">
        <CustomAdaptorBatches></CustomAdaptorBatches>
    </SfDataManager>
    <GridPageSettings PageSize="20"></GridPageSettings>

    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings>

    <GridColumns>
        <GridColumn Field=@nameof(Batches.BatchID) HeaderText="ID" Width="50" AllowEditing="false"></GridColumn>
        <GridColumn Field=@nameof(Batches.IsMade) Width="30" HeaderText="Made" DisplayAsCheckBox="true" EditType="EditType.BooleanEdit"></GridColumn>
    </GridColumns>
</SfGrid>

@code {}


The startup.cs already contains this as other threads have indicated a possible fix 

            services.AddControllers().AddNewtonsoftJson(options =>
            {
                // Use the default property (Pascal) casing
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });


Thanks!
Corey




3 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team November 13, 2020 12:18 PM UTC

Hi Corey,  

Thanks for contacting Syncfusion support.  

Query: “I am getting the following error when I use the search box in a grid in a Blazor application using 18.3.0.48 controls when the grid has a boolean field. 

We have analyzed the reported query by preparing a sample using your code example and we are not able to reproduce the reported issue at our end. Please find the sample from below which we have prepared sample using your code example   


After referring the sample, if you are still facing the issue. kindly get back to us with following details.  

  1. Share the code example of CustomAdaptor.
  2. Share screenshot of the exception detail with full stack trace.
  3. If possible share the issue reproducible sample or try to reproduce the reported issue in provided sample.

Above requested details will be very helpful for us to validate the reported query at our end and provide solution as early as possible.    

Regards, 
Vignesh Natarajan  
 


Marked as answer

CO Corey November 13, 2020 01:53 PM UTC

Thank you for your investigation.

I agree it works when the data source is a static list:

{System.Collections.Generic.List`1[DataGrid.Pages.Index+Batches].Where(Batches => (Batches.BatchID.ToString().ToLower().Contains("a") Or Batches.IsMade.ToString().ToLower().Contains("a")))}

The real world issue is that it does not work when you are using a DBSet -- EntityFramework and SQL Server (Azure in this case) as the data source:

{Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<MC.DataModel.Batches> }

The workaround is to set AllowSorting="false" on the bool field on the grid in case anyone else bumps into this.  I have included a screenshot to show its in the EF Core.

Thank you for the excellent controls!
Corey








JP Jeevakanth Palaniappan Syncfusion Team November 16, 2020 02:23 PM UTC

Hi Corey, 

Thanks for the update. We are glad to hear that you have achieved your requirement on your own. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon