I have a IQueryable<BaseEntity>
and there are derived entities EntityA
and EntityB
. I want my query to return ALL EntityA
objects and only those EntityB
objects which have a certain property not set to a certain value.
Unfortunately, the following code:
IQueryable<BaseEntity> baseEntities = GetBaseEntities();
var result = baseEntities.Where(x => !(x is EntityB) || ((EntityB) x).Prop != MyConstants.Value).ToList();
Throws an error:
NotSupportedException: Unable to cast the type 'BaseEntity' to type 'EntityB'. LINQ to Entities only supports casting EDM primitive or enumeration types.
How to overcome this problem?