如何检查var的null值?

时间:2022-11-15 17:07:20

I am using PetaPoco Micro-ORM with C# 4.0.

我正在使用PetaPoco Micro-ORM和C#4.0。

The code below retrieves a single row from the database:

下面的代码从数据库中检索一行:

var result = db.SingleOrDefault<TdUsers>(getUserQuery);

I would like to check whether or not the result contains any rows, and whether is null. What is the best way to do this?

我想检查结果是否包含任何行,以及是否为null。做这个的最好方式是什么?

4 个解决方案

#1


18  

if (result == null || result.Count() == 0) {
    // Checks whether the entire result is null OR
    // contains no resulting records.
}

I think the problem is not in your check for null, because linq is lazy loading. Your error is in using the expression db.SingleOrDefault<TdUsers>(getUserQuery);.

我认为问题不在于检查null,因为linq是延迟加载的。您的错误在于使用表达式db.SingleOrDefault (getUserQuery);.

.Single<T>(expression) does not return null - it errors if the result returns no values. .SingleOrDefault<T>(expression), however, returns a null value if the expression results in no values - and therefore is best combined with an if (result == null) type check, as you're using here.

.Single (表达式)不返回null - 如果结果没有返回值,则会出错。但是,如果表达式没有结果值,则单个或默认值 (表达式)返回空值 - 因此最好与if(结果== null)类型检查结合使用,就像您在此处使用的那样。

#2


3  

var result = db.SingleOrDefault<TdUsers>(getUserQuery);

In above code SingleOrDefault will return null vale or the specified generic type(it's known on runtime).

在上面的代码中,SingleOrDefault将返回null值或指定的泛型类型(在运行时已知)。

Inorder to check whether the returned values is null or not you can simply use

为了检查返回的值是否为null,您可以简单地使用

if(result!=null)
{
//do your code stuff 
}
else
{
//stuff do be done in case where result==null
}

#3


3  

You could do:

你可以这样做:

result.ToList() // Convert result to a list

if (result.Any()) {
   // result is not null
}

#4


1  

 var v = result.ToList();

now check

if (v.Count > 0)

#1


18  

if (result == null || result.Count() == 0) {
    // Checks whether the entire result is null OR
    // contains no resulting records.
}

I think the problem is not in your check for null, because linq is lazy loading. Your error is in using the expression db.SingleOrDefault<TdUsers>(getUserQuery);.

我认为问题不在于检查null,因为linq是延迟加载的。您的错误在于使用表达式db.SingleOrDefault (getUserQuery);.

.Single<T>(expression) does not return null - it errors if the result returns no values. .SingleOrDefault<T>(expression), however, returns a null value if the expression results in no values - and therefore is best combined with an if (result == null) type check, as you're using here.

.Single (表达式)不返回null - 如果结果没有返回值,则会出错。但是,如果表达式没有结果值,则单个或默认值 (表达式)返回空值 - 因此最好与if(结果== null)类型检查结合使用,就像您在此处使用的那样。

#2


3  

var result = db.SingleOrDefault<TdUsers>(getUserQuery);

In above code SingleOrDefault will return null vale or the specified generic type(it's known on runtime).

在上面的代码中,SingleOrDefault将返回null值或指定的泛型类型(在运行时已知)。

Inorder to check whether the returned values is null or not you can simply use

为了检查返回的值是否为null,您可以简单地使用

if(result!=null)
{
//do your code stuff 
}
else
{
//stuff do be done in case where result==null
}

#3


3  

You could do:

你可以这样做:

result.ToList() // Convert result to a list

if (result.Any()) {
   // result is not null
}

#4


1  

 var v = result.ToList();

now check

if (v.Count > 0)