Sorting a RadGridView using an unmapped/ignored field with Entity Core

1 Answer 73 Views
GridView
Lee
Top achievements
Rank 1
Veteran
Lee asked on 06 Jan 2023, 09:35 PM

Hello!

I have a ragGridView setup roughly as follows:

modelBuilder.Entity<TblCustomers>(entity =>
{
    ...

    entity.Ignore(e => e.Errors);
});

public partial class TblCustomers
{
    ...

    [NotMapped]
    public string Errors { get; set; }
}

void SetupRGV()
{
    var options = new DbContextOptionsBuilder<Test_Context>();
    options.UseSqlServer(GlobalState.ConnectionString);

    TestContext = new Test_Context(options.Options);

    var view = new QueryableEntityCoreCollectionView<TblCustomers>(TestContext, TestContext.TblCustomers, new Collection<string>() { });
    rgvGrid.ItemsSource = view;
}

This works fine, loads and works as expected, and I can fill the unmapped "Errors" field as need be. However, when I click the top of the "Errors" column in the grid to sort, it throws an Exception (see below), tells me I should set up explicit client evaluation and points me at this Microsoft article: https://learn.microsoft.com/en-ca/ef/core/querying/client-eval. I get the following error:

The LINQ expression 'DbSet<TblCustomers>()
    .OrderBy(t => t.Errors)' could not be translated.

Additional information: Translation of member 'Errors' on entity type 'TblCustomers' failed. This commonly occurs when the specified member is unmapped. 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'.

I did try that, and tried to unmap/ignore the field outright so that it wouldn't be sent to the server, but my problem persists.

I can intercept the sorting event and just cancel it when the Errors column is touched, but I'd really prefer to be able to sort by an unmapped field. Could I intercept the filtering event instead?

Is this something that a RadGridView can do for me using that approach I'm taking, or am I simply out of my depth? If I'm just on the wrong track entirely, just let me know and I'll continue researching - no need to provide a custom solution just for this.

Thanks!

Lee

Marco
Top achievements
Rank 1
Iron
Iron
Iron
commented on 30 Aug 2023, 11:34 PM

Hi Lee, i am facing the same problem. Did you ever found a solution? I dont get the custom sorting work for me. Regards Marco.

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 11 Jan 2023, 12:01 PM

Hello Lee,

Thank you for the provided code snippet.

As the error implies, the evaluation of the OrderBy query cannot be performed on the server due to the unmapped Errors field and has to explicitly be performed on the client.

My only suggestion on how to perform this would be to handle the Sorting event as explained here: Custom Sorting.

As for your inquiry about handling the Filtering event, I could not infer how this will affect the sorting of the unmapped Errors field. Can you please elaborate on this?

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Lee
Top achievements
Rank 1
Veteran
Answers by
Dilyan Traykov
Telerik team
Share this question
or