I’m having some problems running a LINQ query off of a LINQ2SQL class that inherits from a base abstract class.
[DataContract]
public abstract class AbstractClass
{
[DataMember]
public abstract int Foo { get; set; }
}
// LINQ2SQL class.
[DataContract]
public class ConcreteClass : AbstractClass
{
[DataMember]
public override int Foo { ... }
}
The problem occurs when I try to run a LINQ query against my Concrete Class. I get a SystemException saying, “Class member AbstractClass.Foo is unmapped.” The query would look something like the following:
// Sample LINQ query.
result = from c in dataContext.ConcreteClasses where c.Foo == 42 select c;
Notice how the query is against CocnreteClass, but the error is on the AbstractClass. Any ideas whats going on here?
It looks like the compiler was having problems infering the correct type. Instead of using the class returned by the LINQ2SQL context, it was using the base class, so I had to cast it to the correct type explicitly.
This previous example does NOT work:
This example does work:
Odd, but it works. Upvotes to anyone who can figure out why it would do that.
(Reference: Very strange inheritance behavior.)