Search code examples
linq

Ignore part of the linq query if variable equals a specific value


       var longlinq = viewModel.Where(x => (x.Systems.Storage == SelectOption || x.Systems.Laptop == SelectOption ||
                  x.Systems._2In1 == SelectOption ||
                  x.Systems.Convertible == SelectOption ) 
                  ||
                  (x.Component.Components == SelectOptionComp ||
                  x.Component.Boards == SelectOptionComp)
                  ||
                 ( x.Service.Services == SelectOptionSer ||
                  x.Service.DevelopmentTools_andServices == SelectOptionSer ) 
                  ||
                  (x.Software.Softwares == SelectOptionSoft ||
                  x.Software.Analytics == SelectOptionSoft) 
                  ||
                  (x.Application.Applications == SelectOptionApp ||
                  x.Application.PrintImaging_andOfficeAutomation ));

Let me explain my question with an example: For instance SelectOptionComp equals "-", then I want to ignore the parts where I used SelectOptionComp in longlinq or set SelectOptionComp to " " in the longlinq. I don't want to use ifs because of large number of combinations. How do I do that?


Solution

  • I have used ternary operator Use the ternary operator: (SelectOptionComp == "-" ? true : (x.Component.Components == SelectOptionComp || x.Component.Boards == SelectOptionComp))