0

I'm coding a search for an MVC app we're building, the thing is I would like to search by various properties of an object. In this particular case this is my expected behavior:

  • If both parameters are null or empty, return all.
  • If any parameter has a value, select all filtered by that parameter using Contains.

This is what I'm doing:

var model = _svc.GetList(q => q.Name == (string.IsNullOrEmpty(entity.Name) ? q.Name : entity.Name) &&
                              q.Description ==  (string.IsNullOrEmpty(entity.Description) ? q.Description : entity.Description));

This returns either all elements if both fields are null or empty, or any element that matches exactly the Name AND/OR Description.

The thing here is I would like this to behave as a Contains.

I've managed to get this working with one field:

var model = _svc.GetList(q => (string.IsNullOrEmpty(entity.Name) || 
            q.Name.ToLower().Contains(entity.Name.ToLower()))).ToList();

But when I add the Description field, it throws a NullReferenceException: Object reference not set to an instance of an object.

Just to check I've tried this and it didn't work either:

var model = (from q in _svc.GetList()
             where (string.IsNullOrEmpty(module.Name) ||
                 q.Name.ToLower().Contains(module.Name.ToLower()))
             select q).ToList();

model = (from q in model
         where (string.IsNullOrEmpty(module.Description) ||
             q.Description.ToLower().Contains(module.Description.ToLower()))
         select q).ToList();

2 Answers 2

3

Well, for a multi-optional-criteria search, you can do (asserting "module" is your "search class").

Simple, and (I think) more readable. Add a null check for the description and entity property of your model.

//take all elements
var model = _svc.GetList();

if (!string.IsNullOrEmpty(module.Description))
  model = model.Where(m => 
                     m.Description != null && 
                     m.Description.ToLower().Contains(module.Description.ToLower());

if (!string.IsNullOrEmpty(module.Name))
  model = model.Where(m => 
                     m.Name != null && 
                     m.Name.ToLower().Contains(module.Name.ToLower());

return module.ToList();

Null check, because a ToLower() on a null will raise a NRE !

Sign up to request clarification or add additional context in comments.

Comments

1

This is a little ugly but should do the trick, you are getting the null reference because of entries with empty Description. If what you are doing with the Name is any clue, you are probably doing something like this q.Description.ToLower().Contains(..) without checking that q.Description is not null

var model = _svc.GetList(q =>  
     (string.IsNullOrEmpty(entity.Name) || string.IsNullOrEmpty(q.Name) 
       || q.Name.ToLower().Contains(entity.Name.ToLower()))

 && (string.IsNullOrEmpty(entity. Description) || string.IsNullOrEmpty(q. Description) 
       || q.Description.ToLower().Contains(entity. Description.ToLower())))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.