views:

33

answers:

2

I was wondering how i compared to an array of ids in EF1.0

I know in EF4.0 Beta 1 there is a contains methods so it would look something like this

int[] associationIds = GetAssociationIds();
from c in Associations where c.AssociationId.Contains(associationIds) select c;

But how do you do the equivalent in EF1.0

+1  A: 

There's no built-in way to do this in EF1. The most commonly used tool for this task is PredicateBuilder.

The solution (using that toolkit) is to build a custom expression that tests AssociationId against each of the ids in your integer array. The resultant expression looks something like this:

int[] associationIds = GetAssociationIds();

// use PredicateBuilder to build this expression using the contents of
// associationIds:

Expression<Func<Association, bool>> testIds = 
    c => c.AssociationId == 1 || c.AssociationId == 2 || c.AssociationId == 5;

And to use it:

var matchingAssociations = db.Associations.Where(testIds);
Ben M
A: 

The predicatebuilder documentation lists a way to do it without the linqkit so i used that method, its not pretty but it will do the job until EF4.0 comes along. Cheers