views:

1076

answers:

2

I really like Entity Framework, but there are some key pieces that are a challenge to me. Can anyone tell me how to filter an EntityDataSource on an Association column? EF hides the FK values and instead has an Association property. Given an Entity, Person, with a PersonType association, I would have expected something like this to work if I want to filter my Person Entity by Type:

GridDataSource.EntityTypeFilter = "it.PersonType.PersonTypeID = 1";

or

GridDataSource.Where = "it.PersonType.PersonTypeID = '1'";

or even

GridDataSource.WhereParameters.Add(new Parameter("it.PersonType.PersonTypeID", DbType.Object, "1"));

but none of those work. Anybody know how to do this?

A: 

Have you tried applying the filter in memory using LINQ? (or Perhaps against the database?)

var personType = new PersonType { Id = 1 };
var query = PersonDataSource.Where(p => p.PersonType.Equals(personType));
// use this query as the DataSource for your GridView

I must admit I haven't done anything like this, but I have used this trick to update/create an entity without loading the associated entities first.

Davy Landman
+1  A: 

I think the answer you're looking for involves using the Include method, such as:

entities.it.Include("PersonType").Where(a => a.PersonType.PersonTypeID = '1');
Keck
That looks promising. I'll have to set up another test for this, since in frustration we yanked EF out and put L2S in. Replaced 6 weeks or so EF hair-pulling in one day with L2S
jlembke