简体   繁体   中英

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?

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 IEnumerable and use Any` to see if it contains record. You can try:

if ((course as IEnumerable<object>).Any())
{
    //records found
}
{
    //no record found
}
if (course != null && (course as IEnumerable<object>).Any())
{
}

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

if (course  is IList)
{

}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM