在实体框架中向实体添加或条件

Bla*_*ell 6 entity-framework entity-framework-4

你能在实体框架中为实体添加"或"条件吗?例如:

Property1 ==(1或2或3)

将值设置为"1 || 2 || 3"或"1,2,3"或"1或2或3"时收到的消息将返回以下消息:

condition is not compatible with the type of the member
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 12

你需要这样做:

var results = entityCollection.Where(entity => entity.Property1 == 1 || entity.Property1 == 2 || entity.Property1 == 3);
Run Code Online (Sandbox Code Playgroud)

  • 如果我想在循环中连接“或”以获得某些属性怎么办? (2认同)

itc*_*chi 10

您还应该查看谓词构建器:http: //www.albahari.com/nutshell/predicatebuilder.aspx

它有点先进,但如果你必须动态连锁条件,这是你最好的选择.

foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
Run Code Online (Sandbox Code Playgroud)