DataQuery - "Where" only for not NULL fields

Konry Lekreso asked on April 21, 2019 01:44

Hello,

I'm trying to build search functionality on my page. I've created form and results table. Everything works fine until I'm trying to search for more than one ".Where" condition. How can I add dynamically another ".Where" if session for field has some value and is not empty? That's my query in custom webpart:

 DataSet memberResults = new DataQuery("store.member.allMembers")
  .Columns("store_member.firstName", "store_member.lastName", "store_member.emailAddress", "store_member.displayName")
  .Where("store_member.firstName", QueryOperator.Equals, firstName)
  //I want to add this another ".Where" if "lastName" field has some value
  //.Where("store_member.lastName", QueryOperator.Equals, lastName)
  .Execute();

Correct Answer

Roman Hutnyk answered on April 22, 2019 16:27

You could build your query in multiple steps - anyway it gets executed after calling .Execute() method.

This means you could do it like that:

var query = new DataQuery("store.member.allMembers")
  .Columns("store_member.firstName", "store_member.lastName", "store_member.emailAddress", "store_member.displayName")
  .Where("store_member.firstName", QueryOperator.Equals, firstName);

  if(!String.IsNullOrEmpty(lastName)){
    query = query.Where("store_member.lastName", QueryOperator.Equals, lastName);
  }


  DataSet memberResults = query.Execute();
0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on April 22, 2019 08:09

Have you tried .WhereNotEmpty() method?

See all available methods here.

1 votesVote for this answer Mark as a Correct answer

Konry Lekreso answered on April 22, 2019 16:20

It occures some issues. Should I add some additional things here? My custom kentico query looks like: SELECT ##COLUMNS## FROM store_md_member WHERE ##WHERE##

0 votesVote for this answer Mark as a Correct answer

Konry Lekreso answered on April 22, 2019 17:37

That works, Thank you Roman!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.