C# 如何检查对象的空值

C# 如何检查对象的空值,c#,asp.net,C#,Asp.net,我使用一个对象类型变量来存储查询结果,以便绑定到下拉列表。如果对象为null,我不希望对其进行进一步处理 我的代码是: object course; if (GetWebsiteCurrentMode().ToLower() == "demo") { course = from t in context.CourseTestDetails join c in context.Courses on t.CourseID equals c.ID

我使用一个对象类型变量来存储查询结果,以便绑定到下拉列表。如果对象为
null
,我不希望对其进行进一步处理

我的代码是:

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
}

如何检查对象类型变量是否为null/empty?

您的对象
课程
永远不会为null,它可能包含记录,也可能不包含记录。由于要在
对象
中返回结果,因此应将其强制转换为IEnumerable
,并使用
Any`查看它是否包含记录。您可以尝试:

if ((course as IEnumerable<object>).Any())
{
    //records found
}
{
    //no record found
}
if((课程为IEnumerable).Any())
{
//找到的记录
}
{
//找不到记录
}

查询不是空的,而是空的。但由于您使用的是对象,因此无法使用
可枚举.Empty
。您可以使用以下技巧从中获取多个
IEnumerable的一个推断类型变量


这里有一个简单的例子来演示它的工作原理:

我猜它将返回一个空的可枚举项,并且不为null,因此您可以通过@V4Vendetta检查如何检查空的可枚举项?@pwn您不需要如我的回答中所示的if语句。@Serge,是的。将其强制转换为单独的变量并检查其是否为null(以查看强制转换是否成功)可能更好,但关键是将其强制转换为
IEnumerable
,然后使用
Any
是否使用比使用Count()更相关的变量?@Habib只是想知道将其强制转换为
IEnumerable
,不要认为对象是正确的候选人,但也许我错了,请让我知道,如果我失踪something@Serge,是的,请参见:@V4Vendetta,我使用casting to object的唯一原因是OP正在投影到匿名类型,
if (course != null && (course as IEnumerable<object>).Any())
{
}
if (course  is IList)
{

}
static IEnumerable<T> SequenceByExample<T>(T t){ return null; }
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
}
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()) ...