NULL
NULL

Reputation: 1589

where if statement in linq query c#

I have a linq query that works perfectly, however, I would like to avoid the whole "where" filter if I have an empty mySTRINGVAR, however when I included if statement it broke the query! thanks in advance for your help.

So this is what I have and this works perfectly!!:

var records = from school in schools
   join tableA in tableAs on someid equals anotherid into tableC
   from tableD in tableC.Where(c => c.tablefield == mySTRINGVAR).DefaultIfEmpty()
   select new { etc.. }

However, I am trying to not include any "where" statement if my mySTRINGVAR is null or empty:

var records = from school in schools
   join tableA in tableAs on someid equals anotherid into tableC
   from tableD in tableC.DefaultIfEmpty()
   select new { etc.. }

Upvotes: 1

Views: 1482

Answers (1)

Habib
Habib

Reputation: 223257

however, i am trying to not include any "where" statement if my mySTRINGVAR is null or empty:

Modify the Where like:

tableC.Where(c => !string.IsNullOrEmpty(mySTRINGVAR) && c.tablefield == mySTRINGVAR)

Upvotes: 5

Related Questions