Search code examples
c#linq-to-sqllambdaexpressionnullable

Lambda expression with <bool> and System.Nullable<bool>


This can't compile:

void Foo()
{
    using (var db = new BarDataContext(ConnectionString))
    {
        // baz is type 'bool'
        // bazNullable is type 'System.Nullable<bool>'
        var list = db.Bars.Where(p => p.baz && p.bazNullable); // Compiler: 
            // Cannot apply operator '&&' to operands of type
            // 'System.Nullable<bool> and 'bool'
    }
}

Do I really have to make this through two runs, where I first use the as condition and then run through that list with the nullable conditions, or is there a better clean smooth best practice way to do this?


Solution

  • p.bazNullable.GetValueOrDefault()