Saturday, March 14, 2020

Linq Conditional check in where clause


In Linq where clause, include parameter when a value is not NULL, else return the query result.

var qryList = from T1 in Tabel1
              join T2 in Tabel2
              on T1.Id equals T2.Id into grp
              from x in grp
              where T1.Id == ParameterID
              && (!string.IsNullOrEmpty(ParameterName) ? 
                  T1.Name == ParameterName : true)
              select T1.Name;