I faced an exception when i try to cast to derived class; Unable to cast the type '' to type ''. LINQ to Entities only supports casting EDM primitive or enumeration types.
(obj => ((DerivedClass)obj).DerivedProperty == true);
I faced an exception when i try to cast to derived class; Unable to cast the type '' to type ''. LINQ to Entities only supports casting EDM primitive or enumeration types.
(obj => ((DerivedClass)obj).DerivedProperty == true);
Thanks all for your help; I solved the problem by using the as operator instead of direct casting; I don't know the reason but this fixed my issue.
obj => (obj as DerivedClass).DerivedProperty == true;
Your problem is most likely solved by using the IQueryable< TBase >.OfType< TDerived >() method to begin your query, but you should post more details of what you're doing.
My best guess is that you are trying to do the casting before the SQL statement is executed and LINQ to Entities has no idea how to translate this to SQL.
One of your options is to execute the SQL query before you do the casting and then LINQ to Objects should be able to handle this. Warning: This may lead to N+1 problem.
There may be a more clever solution but without more code, this is my best guess.