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 ...
}
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
Can you check the following forum? The error sounds similar to yours.