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?

4
  • 1
    I guess it would return a Empty enumerable and not null, so maybe you could check via .Any() Commented Apr 16, 2013 at 12:12
  • @V4Vendetta how to check for Empty enumerable ? Commented Apr 16, 2013 at 12:15
  • stackoverflow.com/questions/6417902/… Commented Apr 16, 2013 at 12:22
  • @pwn you don't need the if statement as shown in my answer. Commented Apr 16, 2013 at 12:31

4 Answers 4

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

@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<T> and then use Any
Is using Any more relevant than using Count()?
@Habib was just wondering about the cast to IEnumerable<object>, 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, the only reason I used casting to object is that the OP is projecting to anonymous type,
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)
{

}

Comments

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

Comments

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

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.