Systemic33
Systemic33

Reputation: 35

How can i switch from a boolean switch an OR operator on/off

I'm making Linq code to query a database, and depending on a boolean, i need to switch between using a statement and using a !statement (ie. NOT), how can i do this without using the below if/else which would mean i have very identical code two places.

See the following example:

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

It has to be usable for a LINQ query, and the above example is simplified down to the essence of the problem.

Thanks in advance!

Upvotes: 1

Views: 148

Answers (1)

Danilo Vulović
Danilo Vulović

Reputation: 3063

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

Upvotes: 5

Related Questions