Conditional WHERE in LINQ

前端 未结 1 1646
孤城傲影
孤城傲影 2021-01-12 21:30

Hi any suggestions on building a LINQ statement based on search criteria?

I\'ll be passing in an instance of a \'SearchCriteria\' class with all parameters nullable.

相关标签:
1条回答
  • 2021-01-12 22:21

    Try:

    .Where(x =>
        (x.a != null ? x.a == a : false) &&
        (x.b != null ? x.b == b : false));
    

    or

    .Where(x =>
        (x.a != null && x.a == a) ||
        (x.b != null && x.b == b));
    

    Also:

    .Where(x => new int[] { 1, 2, 3 }.Contains(x.i));
    
    0 讨论(0)
提交回复
热议问题