Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Or Condition to Entity in Entity Framework

Can you add an "Or" condition to an entity in the entity framework? For example something like:

Property1 == (1 or 2 or 3)

The message I get when putting the value of "1 || 2 || 3" or "1,2,3" or "1 or 2 or 3" returns this message:

condition is not compatible with the type of the member
like image 214
Blake Blackwell Avatar asked Apr 02 '10 19:04

Blake Blackwell


2 Answers

You need to do:

var results = entityCollection.Where(entity => entity.Property1 == 1 || entity.Property1 == 2 || entity.Property1 == 3);
like image 77
Reed Copsey Avatar answered Sep 19 '22 22:09

Reed Copsey


You should also check out predicate builder: http://www.albahari.com/nutshell/predicatebuilder.aspx

It's a bit more advanced, but if you have to dynamically chain conditions it's your best bet.

foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
like image 32
itchi Avatar answered Sep 20 '22 22:09

itchi