I have an abstract class that is inherited by two classes. Both classes represent tables in the database. I am having troubles mapping expressions though in the abstract class and therefore I keep getting exceptions that it cannot be translated to SQL. All questions I found in Stackoverflow are talking about columns which is already working for me.
Below is a very simple code that shows what I mean. Car and Motorcycle have completely separate implementations of _isNew Expression.
public abstract class Vehicle {
public abstract boolean IsNew;
}
public partial class Car: Vehicle {
public override boolean IsNew {
get { _isNew.Invoke(this); }
}
}
public partial class Motorcycle: Vehicle {
public override boolean IsNew {
get { _isNew.Invoke(this); }
}
}
I would like to be able to call either _isNew expression or IsNew property on an IQueryable and yet it runs the _isNew of the Car class in case Vehicle is type of Car. Is there anyway I can accomplish that? All solutions I tried caused an exception that it cannot be translated to SQL.
_isNew
? Can't is be an expression that can be translated? – RopeIsNew
as:public abstract bool IsNew(int age);
. Then implementation:public override bool IsNew(int maxAge){return this.Age<maxAge;}
. Finally, usage:Car c = new Car(){Age = 9}; Console.WriteLine("Car.Age={0}. Is new={1}", c.Age, c.IsNew(5));
– Subcortex