1

The action method in the controller receive a value, like a couple of letters. The code checks if the table contains any of this letters. I'm using this code for that task:

var result = db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper()));

But how can I check if the result variable is empty or null, when there are not any matching letters? I tested this, but it's not working!

If(result == ""){
// Do something
}

I would like to use Viewbag to send a message that there was no match or perhaps do this check in the View. I'm using this with some AJAX and Partial Views and it's working perfect, but I just want show a message if there are not any matches. What is the best way to check if the result value is empty or null?

3
  • what is result type? Commented Mar 9, 2016 at 10:27
  • if(result != null){ // all good Commented Mar 9, 2016 at 10:28
  • Hint: check the return value type of IEnumerable.Where<TSource>. Commented Mar 9, 2016 at 10:28

4 Answers 4

4

The simplest way would be by using !result.Any():

var result = db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper()));

If(!result.Any()){
    // Do something
}

From MSDN on IEnumerable:

Any()

Determines whether a sequence contains any elements.

Fits exactly what you need.

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

1 Comment

This out of all the answers i think is the best as it answers the question with a simple solution, provides references, and makes intended use of a feature--other solutions require (to me) extra work. +1
1

Try this, Using FirstOrDefault it will give you the first record from the result, else if no result is yielded from the query it returns default value that is null,

  var result = db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper())).FirstOrDefault();

  If(result == null){
    // Do something
  }

Or if you want to use this result, to manipulate something in your code then you can use the ToList(). It will return you list of values and if the query didnt yield anything then the list count will be 0.

 var result = db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper())).ToList();

 If(result.Count == 0){
    // Do something
 }

Comments

1

Your code is not working because the code is returning an object not a string if you want to return a string then you have to use the "Select" clause to select a specific field and if you want to check in the same code then modify :

var result = db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper())).ToList(); if(result != null && result.Count > 0)

it will work for you.

Comments

1

For IEnumerable we can use Any(), your code will can be written as

if(!db.People.Where(b => b.Name.ToUpper().Contains(filter.ToUpper())).Any() {
// do some thing
}

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.