'Filtering on columns with Conversion
I have a struct that wraps a primitive column value in the database, and I want to apply a filtering query (i.e. a Linq where translated to a SQL where):
Toy example:
struct Dollars
{
public Dollars(decimal amount) => Amount = amount;
public decimal Amount { get; }
override ToString => $"${amount}";
}
EF mapping:
modelBuilder.Entity<Item>()
.Property(k => k.Price)
.HasConversion<decimal>(d => d.Amount, a => new Dollars(a));
Example query:
var expensive = dbContext.Items.Where(x => x.Price.Amount > 1000).ToList();
It seems navigating to properties on my struct (x.Amount) in the filtering is not possible.
System.InvalidOperationException : The LINQ expression 'DbSet().Where(t => t.Price.Amount > 1000)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation [...]
Is it possible at all to accomplish this somehow, or does primitive Conversion basically only work with identity/equality and not comparison?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
