Pawan
Pawan

Reputation: 1744

How to check an object for null values

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?

Upvotes: 2

Views: 2457

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460288

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

Upvotes: 1

Serge
Serge

Reputation: 6712

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()) ...

Upvotes: 0

Satpal
Satpal

Reputation: 133453

if (course != null && (course as IEnumerable<object>).Any())
{
}

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

if (course  is IList)
{

}

Upvotes: 2

Habib
Habib

Reputation: 223402

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
}

Upvotes: 3

Related Questions