lundi 23 juillet 2018

LINQ Conditional Where clause including multiple conditions

I'm trying to build out a LINQ statement that accounts for two different conditions in the where clause and I haven't found a solution on here specific to what I'm trying to do.

I have a list of process steps that I'm trying to query as such:

stepsQuery = _context.ProcessSteps
    .Where(a.StepType == Constants.ProcessStepTypes.Standard)

if (includeA)
    stepsQuery = stepsQuery.Where(u => u.StepType == Constants.ProcessStepTypes.A);

if (includeB)
    stepsQuery = stepsQuery.Where(u => u.StepType == Constants.ProcessStepTypes.B);

I have two variables that are passed in, includeA and includeB. I need all of the standard steps, but also A steps if includeA is true and also B steps if includeB is true. I'm trying to put this all into one statement if it's possible. I've been playing with 'contains' but I can't quite get this to work.

Aucun commentaire:

Enregistrer un commentaire