I have an entity represeting exercise training set:
public partial class TrainingSetEntity
{
public int? RepCount { get; set; }
public double? WeightLiftedKilograms { get; set; }
}
and I want to add custom property that doesn’t represent column from the table but is calculated based on RepCount
and WeightLiftedKilograms
. And I want to use this property directly in LINQ queries.
This old blog was describing a way how to achieve this, but when I tried that, EF wasn’t able to translate the query.
I have tried:
public partial class TrainingSetEntity
{
[NotMapped]
public double EstimatedOneRepMaxBrzycki => EstimatedOneRepMaxFunc(this);
private static Expression<Func<TrainingSetEntity, double>> EstimatedOneRepMaxExpression = tSet =>
tSet.WeightLiftedKilograms == null || tSet.RepCount == null
? 0D
: tSet.WeightLiftedKilograms.Value * (36D / (37D - (tSet.RepCount.Value > 10 ? 10 : tSet.RepCount.Value)));
private static Func<TrainingSetEntity, double> EstimatedOneRepMaxFunc = EstimatedOneRepMaxExpression.Compile();
}
Does is still work in new EF Core
(we use EF Core 8
) ? Or are there any alternatives to do use custom property in query like:
await Context.Set<TrainingSetEntity>()
.OrderByDescending(tSet => tSet.EstimatedOneRepMaxBrzycki)
.ToListAsync(cancellationToken);