相关疑难解决方法(0)

如何在 LINQ 中的几个 where 条件之间执行“OR”操作

我正在使用 IQueryable 来构建我的查询。

 IQueryable<PropertyPosts> posts = context.PropertyPosts;
Run Code Online (Sandbox Code Playgroud)

之后,基于几个条件,我将Where子句附加到我的查询中,

 if (item.ApartmentCondo == 1)
 {
    posts= posts.Where(x => x.name == item.name &&
                            x.PropertyType == PropertyType.ApartmentCondo );

 }
 if (item.House == 1)
 {
  posts= posts.Where(x => x.name == item.name &&
                            x.PropertyType == PropertyType.House );

 }
Run Code Online (Sandbox Code Playgroud)

注意:还有其他几个 where 条件。

之后,当我执行以下查询时,

 List<PropertyPosts> posts2 = posts.ToList();
Run Code Online (Sandbox Code Playgroud)

上述所有的地方条件将是“AND后。

但相反,我需要将 Where 条件进行“或”运算。

这意味着我需要一种方法来附加几个 Where 条件,但所有这些条件都应该在它们之间执行“OR”条件。

我怎样才能做到这一点?有没有替代方法而不是使用“ Where ”?

c# linq linq-to-entities entity-framework

5
推荐指数
1
解决办法
2006
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1

linq-to-entities ×1