如何检查VAR为空值? [英] How to check a var for null value?

查看:306
本文介绍了如何检查VAR为空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PetaPoco微ORM用C#4.0。

在code以下检索单列从数据库中:

  VAR的结果= db.SingleOrDefault< TdUsers>(getUserQuery);
 

我想检查结果是否包含任何行,以及是否为空。什么是做到这一点的最好方法是什么?

解决方案

 如果(结果!= NULL || result.Count()== 0){
    //检查是否整个结果为空或
    //不包含任何结果记录。
}
 

我觉得这个问题是不是在你的支票,因为LINQ是延迟加载。你的错误是在使用前pression db.SingleOrDefault< TdUsers>(getUserQuery);

。单< T>(EX pression)没有返回null - 它的错误,如果结果没有返回值。 .SingleOrDefault< T>(EX pression),然而,返回空值,如果EX pression,不会产生任何价值 - 因此最好用结合一个如果(结果== NULL)类型检查,因为你使用的是在这里。

I am using PetaPoco Micro-ORM with 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?

解决方案

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

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

这篇关于如何检查VAR为空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆