Add or condition for an object in Entity Framework

Can you add an “Or” condition to an entity in an entity’s infrastructure? For example, something like:

Property1 == (1 or 2 or 3)

The message that I get when setting the value "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
+5
source share
3 answers

You need to do:

var results = entityCollection.Where(entity => entity.Property1 == 1 || entity.Property1 == 2 || entity.Property1 == 3);
+11
source

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

This is a bit more advanced, but if you need to dynamically link conditions, this is best.

foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
+10

Not tried, but you can try to use contains. Not sure about performance, but less code:

int[] vals = new int[] { 1, 2, 3, 4 };
var results = entityCollection.Where(entity => vals.Contains(entity.Property1));
+3
source

All Articles