1

I have the following code:

       var questionStatuses =
           (
               from questionStatus in this._questionStatusService.GetQuestionStatuses()
               select new
               {
                   id = questionStatus.QuestionStatusId,
                   status = questionStatus.Status
               }
           );
        if (questionStatuses == null || questionStatuses.length() == 0)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        else
        {
            var response = Request.CreateResponse(HttpStatusCode.OK, questionStatuses);
            return response;
        }

questionStatuses is an IEnumrable.

How can I check to ensure this has content as is not just an empty array? I tried .length() but it gives an error. Also this is part of a WebAPI controller so is it okay to return IEnumerable or should I make it into a list and if so how can I do that?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

6 Answers6

3

questionStatuses is an IEnumrable. How can I check to ensure this has content as is not just an empty array?

Just use Any

questionStatuses.Any()
EZI
  • 15,209
  • 2
  • 27
  • 33
2

use .Count() method instead of .length()

code

  if (questionStatuses == null || questionStatuses.Count() == 0)
    {
        return Request.CreateResponse(HttpStatusCode.NotFound);
    }
    else
    {
        var response = Request.CreateResponse(HttpStatusCode.OK, questionStatuses);
        return response;
    }
Cris
  • 12,799
  • 5
  • 35
  • 50
2

You can use .Count() from System.Linq to get the length. However you typically would not want to do that as it would force the entire enumerable to be realised. (Also note that not all enumerables will support being enumerated more than once)

If you have to convert to a list then look at the .ToList() extension method.

Typically you should return return the IEnumerable if you were given one

Also see other answers already on SO (e.g. IEnumerable doesn't have a Count method). Again I would suggest that you first consider if you really need the count.

Finally take a look at other answers showing how to check if the enumerable is empty (e.g. Recommended way to check if a sequence is empty) using .Any()

Community
  • 1
  • 1
Andre
  • 4,560
  • 2
  • 21
  • 27
1

Use Count

questionStatuses.count()

MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
1

Use count()

Try this code, instead of your code

var questionStatuses =
           (
               from questionStatus in this._questionStatusService.GetQuestionStatuses()
               select new
               {
                   id = questionStatus.QuestionStatusId,
                   status = questionStatus.Status
               }
           );
var response =(questionStatuses==null || questionStatuses.count() == 0) ? Request.CreateResponse(HttpStatusCode.NotFound):Request.CreateResponse(HttpStatusCode.OK, questionStatuses);
return response ;

or

Update

try this

 var questionStatuses =
               (
                   from questionStatus in this._questionStatusService.GetQuestionStatuses()
                   select new
                   {
                       id = questionStatus.QuestionStatusId,
                       status = questionStatus.Status
                   }
               );
    var response = !questionStatuses.Any() ? Request.CreateResponse(HttpStatusCode.NotFound):Request.CreateResponse(HttpStatusCode.OK, questionStatuses);
    return response ;
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Do you think I could replace the check for null by using .any() only ? – Samantha J T Star Feb 26 '14 at 08:02
  • The `Any()` return true if the source sequence contains any elements; otherwise, false. You need just put `!questionStatuses.Any()` instead of `(questionStatuses==null || questionStatuses.count() == 0)` – Ramesh Rajendran Feb 26 '14 at 08:04
0

Since all you want to know is whether the enumerable is empty, you can do it like this:

if (questionStatuses == null || !(questionStatuses.GetEnumerator().MoveNext())){

This will not require the full IEnumerable to be enumerated - which could be costly, otherwise.

Sebastian
  • 7,729
  • 2
  • 37
  • 69