QueryableEntityCoreCollectionView with custom / non database properties sorted with GridView

0 Answers 122 Views
EntityFrameworkCoreDataSource (.Net Core)
Marco
Top achievements
Rank 1
Iron
Iron
Iron
Marco asked on 16 Aug 2023, 12:06 AM

Hi,

i have two database models:

public class LogOrder
{
    [Key]
    public long Id { get; set; }

    public string Name { get; set; }
	...

    [Display(AutoGenerateField = false)]
    [InverseProperty(nameof(LogOrderUpdate.Order))]
    public virtual ICollection<LogOrderUpdate> Updates { get; set; }

    #region View

    [Display(AutoGenerateField = false)]
    public LogOrderUpdate LastUpdate
    {
        get
        {
            if (Updates != null && Updates.Any())
            {
                return Updates.MaxBy(x => x.Time);
            }

            return null;
        }
    }
    
    #endregion

     ctor ...
}
and
public class LogOrderUpdate : IEntity
{
    [Key]
    public long Id { get; set; }

    public DateTime Time { get; set; }
    ...
}

used in a ViewModel

private QueryableEntityCoreCollectionView<LogOrder> entityCollectionView;

public QueryableEntityCoreCollectionView<LogOrder> EntityCollectionView
{
    get { return entityCollectionView; }
    set
    {
        this.entityCollectionView = value;
        this.OnPropertyChanged(nameof(EntityCollectionView));
    }
}
private readonly LogOrderContext objectContext;

public ViewModel()
{
    objectContext = new LogOrderContext();
    entityCollectionView = new QueryableEntityCoreCollectionView<LogOrder>(objectContext, objectContext.LogOrders, new Collection<string>() { nameof(LogOrder.Updates) } );
}

shown in xaml

<telerik:RadGridView 
    AutoGenerateColumns="False"
    ItemsSource="{Binding EntityCollectionView}"
    IsReadOnly="True" 
    Name="LogOrderGrid">
    <b:Interaction.Behaviors>
        <behaviors:RadGridLayoutBehavior />
    </b:Interaction.Behaviors>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Id" UniqueName="Id" DataMemberBinding="{Binding Path=Id}"/>
        <telerik:GridViewDataColumn Header="Name" UniqueName="Name" DataMemberBinding="{Binding Path=Name}"/>
        <telerik:GridViewDataColumn Header="Time" UniqueName="LastUpdateTime" DataMemberBinding="{Binding Path=LastUpdate.Time}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Everything works as expected

But when I try to sort or filter the grid i get a System.InvalidOperationException

System.InvalidOperationException: 'The LINQ expression 'DbSet<LogOrder>()
    .OrderBy(l => l.LastUpdate.Time)' could not be translated. Additional information: Translation of member 'LastUpdate' on entity type 'LogOrder' 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

It tries to query the "non database" property directly from the database -> .OrderBy(l => l.LastUpdate.Time)'

and the grid gets empty

I know combining db model and view properties are not a good approach but with the QueryableEntityCoreCollectionView I have to!?
Is there any annotation for those properties like [NonDbProperty] i can control this behavior?
Or is my approach completely wrong?

thank you in advance,
Marco

Martin Ivanov
Telerik team
commented on 30 Aug 2023, 08:19 AM

Can you check the following forum? The error sounds similar to yours.

No answers yet. Maybe you can help?

Tags
EntityFrameworkCoreDataSource (.Net Core)
Asked by
Marco
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or