收录日期:2020/06/02 20:11:40 时间:2009-08-07 16:19:13 标签:entity-framework,linq-to-entities

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

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);

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