0

I have a large query from which I'm trying to get data using the Where condition with the parameters StudentName and StudentSurname, which is set by the user through a search form.

If StudentName and StudentSurname has values, everything is fine, but if one of them does not have a value, then I receive nothing.

Problem is that in such case I want this foreach loop to get data anyway without the parameters that are null.

foreach (var item in innerJoinQuery.Where(item => item.StudentName == StudentName 
                                               && item.StudentSurname  == StudentSurname)
Aydin
  • 15,016
  • 4
  • 32
  • 42
David
  • 4,332
  • 13
  • 54
  • 93
  • 1
    Do you mean the input values are not provided (i.e. `StudentName` is null) or the data is not there, i.e. `item.StudentName` is null? – Nisarg Shah Oct 04 '18 at 06:07
  • yes. when you hit search but do not fill the search box of StudentName or StudentSurname . – David Oct 04 '18 at 06:09
  • Then you need to return all the data from database? – SilentCoder Oct 04 '18 at 06:09
  • In that case you case just validate like this : string.IsNullOrEmpty(StudentName) which returns boolean value – xoxo Oct 04 '18 at 06:10
  • Yes I want to return data anyway. And if one of variables is null then foreach gives nothing. But I want it to return at least all data with second option – David Oct 04 '18 at 06:11
  • Kamala HB can you write foreach string complete so I can understand ? – David Oct 04 '18 at 06:11
  • try this => `foreach (var item in innerJoinQuery.Where (item => (item.StudentName == null || item.StudentName == StudentName) && (item.StudentSurname == null || item.StudentSurname == StudentSurname) )` – er-sho Oct 04 '18 at 06:12
  • try `item => (item.StudentName == StudentName || string.IsNullOrEmpty(StudentName )) && (item.StudentSurname == StudentSurname || string.IsNullOrEmpty(StudentSurname))` – Just code Oct 04 '18 at 06:13
  • https://stackoverflow.com/questions/7438382/linq-how-to-exclude-condition-if-parameter-is-null this question has your answer – SilentCoder Oct 04 '18 at 06:13
  • thanks all of you for help, I've checked Nisarg Shah solution and it suits me just fine. Thanks all! – David Oct 04 '18 at 06:18
  • @David Can you first tell me which field you ant to check whether it is null or not? whether StudentName or item.StudentName ? Can you post your sample code? – xoxo Oct 04 '18 at 06:19
  • Kamala HB I want to check StudentName and StudentSurname whether they have some value or not, and if they do then use them in foreach. Nisarg Shah wrote solution and it works perfect. I've checked it already. Thank you Sir – David Oct 04 '18 at 06:21
  • Possible duplicate of [Linq: adding conditions to the where clause conditionally](https://stackoverflow.com/questions/10884651/linq-adding-conditions-to-the-where-clause-conditionally) – xdtTransform Oct 04 '18 at 06:21

2 Answers2

4

You could put an OR condition allowing an item to pass if one of the search parameters is null or empty. Something like:

innerJoinQuery.Where(item => 
    (string.IsNullOrWhiteSpace(StudentName) || item.StudentName == StudentName) 
    && 
    (string.IsNullOrWhiteSpace(StudentSurname) || item.StudentSurname  == StudentSurname)
)
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • You can put every `or` condition in the separated `Where` function, will be much more readable – Fabio Oct 04 '18 at 07:41
4
if (!string.IsNullOrEmpty(StudentName))
    innerJoinQuery = innerJoinQuery.Where(item => item.StudentName == StudentName);

if (!string.IsNullOrEmpty(StudentSurname))
    innerJoinQuery = innerJoinQuery.Where(item => item.StudentSurname == StudentSurname);

foreach (var item in innerJoinQuery)
...
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
kaffekopp
  • 2,551
  • 6
  • 13
  • 2
    I find this cleaner than Nisargs solution, easier to read, even though they're both essentially the same – Aydin Oct 04 '18 at 06:17