LINQ WHERE Operation / Ignore

I need to ignore some or all of the conditions in the WHERE statement if the parameter is empty or FE is empty:

I have a simple LINQ query

var query=
from x in context.a
where x.p == param1 && x.i == param2
select x

How to ignore x.p == param1if param1 is null or emty?

EDIT

Tried this

var query =
                    from myLog in myContext.ApsValidationLogs
                    where (myLog.systemtype == comboBoxSystemType.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxSystemType.SelectedItem.ToString()))
                        && (myLog.bankid == comboBoxBankId.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxBankId.SelectedItem.ToString())))
                    select myLog;

But got

Object reference not set to an instance of an object.

In case the second combobox element is NULL. What's wrong?

+5
source share
2 answers

You can add it as a condition:

 var query= from x in context.a 
            where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
            select x;

If param1empty or empty, the condition will always be true, which in fact "completely ignores" the conditions elsewhere.

+8
source

param1 param2 ..

var query = from x in context.a
             where (X.p==param1 || string.IsNullOrEmpty(param1))
             && (X.i==param2 || string.IsNullOrEmpty(param2))
             select x;

,

+5

All Articles