Carolyn Van Slyck

Using Generic Methods in Entity Framework Queries

Many of my models have a common property that I wanted to use in Entity Framework queries. After creating a generic method to encapsulate the filter logic, everything compiled but at runtime I ran into the following error:

Unable to cast the type 'FooBar' to type 'IFooBar'. LINQ to Entities only supports casting EDM primitive or enumeration types.

Thanks to this StackOverflow post, I found the solution. When specifying the constraints on your generic parameter, include "class" as the first constraint. Success!