【问题标题】:How can i switch from a boolean switch an OR operator on/off我如何从布尔开关打开/关闭 OR 运算符
【发布时间】:2012-11-22 11:26:10
【问题描述】:

我正在编写 Linq 代码来查询数据库,并且根据布尔值,我需要在使用语句和使用 !statement 之间切换(即 NOT),如果不使用下面的 if/,我怎么能做到这一点?否则这意味着我在两个地方有非常相同的代码。

请看下面的例子:

private string getFromDB(bool decision)
{
    if (decision)
    {
        return db.Where(p => (Types.Contains(p.CurrentOwner)));
    }
    else
    {
        return db.Where(p => !(Types.Contains(p.CurrentOwner)));
    }
}

它必须可用于 LINQ 查询,上面的示例已简化为问题的本质。

提前致谢!

【问题讨论】:

    标签: c# linq operators boolean-logic


    【解决方案1】:
    private string getFromDB(bool decision)
    {
        return db.Where(p => (Types.Contains(p.CurrentOwner) == decision));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2018-08-28
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多