1
Reply

LINQ Question if-else in where clause

Is it possible to give “if-else” condition in LINQ Where clause?

    Yes, it is possible to use if-else conditions in a LINQ Where clause using the ternary operator (? :) or the null coalescing operator (??).
    List numbers = new List { 1, 2, 3, 4, 5 };
    int max = 3;
    var filteredNumbers = numbers.Where(n => n <= max ? true : false);