2

I am using an object type variable to store a query result for binding to a drop down list. I do not want further processing on an object if it is null.

My code is :

object course;
if (GetWebsiteCurrentMode().ToLower() == "demo")
{
    course = from t in context.CourseTestDetails
            join c in context.Courses
            on t.CourseID equals c.ID                                  
            where t.UserID == UserID && c.IsDeleted == true
            select new
            {
                c.ID,
                c.CourseName
            };

}
else
{
    course = from t in context.CourseTestDetails
            join c in context.Courses
            on t.CourseID equals c.ID                                  
            where t.UserID == UserID  c.IsDeleted == false
            select new
            {
                c.ID,
                c.CourseName
            }
}
if(course !=null )
{
    ddlCourseName.DataSource = course;
    ddlCourseName.DataBind();

    ddlCourseName.Items.Insert(0, new ListItem("Select Course Name", "0"));
    ddlCourseName.SelectedValue = "0";
}
else
{
    //do something different
}

How can I check object type variable for null/empty?

Nate
  • 16,748
  • 5
  • 45
  • 59
Pawan
  • 1,704
  • 1
  • 16
  • 37
  • 1
    I guess it would return a Empty enumerable and not null, so maybe you could check via [`.Any()`](http://stackoverflow.com/questions/1191919/what-does-linq-return-when-the-results-are-empty) – V4Vendetta Apr 16 '13 at 12:12
  • @V4Vendetta how to check for Empty enumerable ? – Pawan Apr 16 '13 at 12:15
  • http://stackoverflow.com/questions/6417902/checking-if-object-is-null-in-c-sharp – Nag Apr 16 '13 at 12:22
  • @pwn you don't need the if statement as shown in my answer. – Serge Apr 16 '13 at 12:31

4 Answers4

3

Your object course would never be null, it may or may not contain record. Since you are getting the results back in object, you should cast it to IEnumerableand useAny` to see if it contains record. You can try:

if ((course as IEnumerable<object>).Any())
{
    //records found
}
{
    //no record found
}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • @Serge, yes. It may be better to cast it to a separate variable and check if its not null *(to see if the casting succeeded)* , but the whole point is to cast it to `IEnumerable` and then use `Any` – Habib Apr 16 '13 at 12:22
  • Is using Any more relevant than using Count()? – Serge Apr 16 '13 at 12:23
  • @Habib was just wondering about the cast to `IEnumerable`, don't think object would be right candidate but then maybe i am wrong please do let me know if i am missing something – V4Vendetta Apr 16 '13 at 12:26
  • 1
    @Serge, yes see: http://stackoverflow.com/questions/305092/which-method-performs-better-any-vs-count-0 – Habib Apr 16 '13 at 12:27
  • @V4Vendetta, the only reason I used casting to object is that the OP is projecting to anonymous type, – Habib Apr 16 '13 at 12:28
2
if (course != null && (course as IEnumerable<object>).Any())
{
}

Optional: Additionally you should also check that object is implements IList interface

if (course  is IList)
{

}
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

The queries are not null but empty. But since you're using an object you cannot use Enumerable.Empty. You can use following trick from E. Lippert to get one inferred-typed variable for multiple IEnumerable<anynymous type>:

Use this method to create a typed variable from an anonymous type:

static IEnumerable<T> SequenceByExample<T>(T t){ return null; }

Now this works:

var course = SequenceByExample(new { ID = 0, CourseName = "" } );
if (GetWebsiteCurrentMode().ToLower() == "demo")
{
    course = from t in context.CourseTestDetails
            join c in context.Courses
            on t.CourseID equals c.ID                                  
            where t.UserID == UserID && c.IsDeleted == true
            select new
            {
                c.ID,
                c.CourseName
            };
}
else
{
    course = from t in context.CourseTestDetails
    // ...
}
if(course.Any())
{
   // ...
}
else
{
    //do something different
}

Declaring an implicitly typed variable inside conditional scope and using it outside

Here's a simple example to demonstrate that it works: http://ideone.com/lDNl4d

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
var course = from t in context.CourseTestDetails
                 join c in context.Courses
                 on t.CourseID equals c.ID                                  
                 where t.UserID == UserID && c.IsDeleted == (GetWebsiteCurrentMode().ToLower() == "demo")
                 select new
                 {
                     c.ID,
                     c.CourseName
                  };

if (course.Any()) ...
Serge
  • 6,554
  • 5
  • 30
  • 56