Saturday, April 10, 2010

EntityDataSource Where with relationships

The EntityDataSource control can be used with a data-bound control to retrieve data from an EDM and to display. Most of the samples just display data based on a single table. Just what I want, but I had one more requirement. I required a filter based on a related table.


 
First let show the EDM modal:

 

I have two tables:
  • Dossier: contains all the dossier in the company.
  • Access: specifies which departments have access to a dossier.

 
My Grid needed to display all the dossier a specific department was the owner (Dossier.OwnerId) of or had access to (Access.DepartementId).

 
In the sample below, I assume the departmentId is 1.

 
The first requirement was very simple: just create a where on the OwnerId like:


But the second requirements cause me trouble. First of all I needed to make sure the Access table was included in my "query". Second the where needed to query on a the Access table which was a collection on the Dossier object (because of the 1 to many relationship).


Solution: