Tuesday, November 06, 2012

Dynamic Entity Framework Filtering

I recently created a library that works against an Entity Framework (V5) model and allows for creation of a central method which accepts:
  • A generic type parameter indicating which entity type to return.
  • Any number of parameters (int or list of ints indicating ids) which will contain ids of related entities.
The library will return generated code for the linq to EF query which when compiled (by passing it to a compiler helper) will return IQueryable (for further filtering if necessary) of the type of entity indicated by the type parameter.  So the method the user of the library would use would look something like:

EntityService.All( ParentEntityId, OtherRelatedId, ListOfYetOtherRelatedIds )

This can greatly reduce the amount of simple inner join queries necessary in your repository.  You can find it at codeplex.  Hope someone else finds it useful.