I have extracted the relevant sections of the codebase to illustrate. Am really stuck on something potentially simple.
for information, in the database there is a one to many relationship between p and entity1.
predicate = PredicateBuilder.False<myType>();
.....
predicate = predicate.And(p => p.entity1.FirstOrDefault().id == 1 || p.entity1.FirstOrDefault().id == 2);
What I really need is to programmatically do the ( p.entity1.FirstOrDefault().id == 1 || p.entity1.FirstOrDefault().id == 2 )
forexample below
List<int> listOfId = new List<int>(){1,2,3,4,5,6,7,8,9};
predicate = predicate.And(p => p.entity1.id.wherein(listOfid);
Is there anyway this is possible please, it is quite critical to me.
Use Contains
instead:
List<int> listOfId = new List<int>(){1,2,3,4,5,6,7,8,9};
predicate = predicate.And(p => listOfid.Contains(p.entity1.id));
In this particular case, the Contains
method (as in @MarcinJuraszek's answer) is the way to go.
However, if you need to mix and match And
s and Or
s, you can do this:
predicate = PredicateBuilder.True<myType>();
//other clauses
.....
List<int> listOfId = new List<int>(){1,2,3,4,5,6,7,8,9};
var orClause = PredicateBuilder.True<MyType>();
foreach (var id in listOfId)
{
orClause = orClause.Or(p => p.entity1.FirstOrDefault().id == id);
}
predicate = predicate.And(orClause);
(lifted from here )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.