Linq search that ignores nulls

c#, linq-to-sql

Solution

IEnumerable<X> query = items;
if (a.HasValue) {
    query = query.Where(x => x.a == a.Value)
}
if (b.HasValue) {
    query = query.Where(x => x.b == b.Value)
}
if (c.HasValue) {
    query = query.Where(x => x.c == c.Value)
}

Problem

How can I make a linq search that ignores nulls (or nullables)? I have a method ``` IEnumerable<X> Search(int? a, int? b, int? c) ``` And I want it to return matches on any of the ints? that are not null. IE: if `a` and `c` have values 1 and 9 and `b` is null the search should render (roughly) to ``` SELECT * FROM [TABLE] WHERE a = 1 AND c = 9 ``` My real method will have 5+ paramters, so iterating combinations is right out.

Original source