0

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?

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • https://stackoverflow.com/questions/18976495/linq-to-entities-only-supports-casting-edm-primitive-or-enumeration-types-with-i – Roman Ryzhiy Sep 07 '20 at 13:20
  • I don't see where I could put the "where T : class" modifier, I don't have any generics in my code.. – D.R. Sep 07 '20 at 13:55

1 Answers1

0

One workaround seems to be the following:

GetAllEntityA().Cast<BaseEntity>().Concat(GetAllEntityB(b => b.Prop != MyConstants.Value))

I'm still looking for a better answer though.

D.R.
  • 20,268
  • 21
  • 102
  • 205