{"id":3907,"date":"2014-03-30T06:19:15","date_gmt":"2014-03-30T06:19:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-check-a-var-for-null-value-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:19:15","modified_gmt":"2014-03-30T06:19:15","slug":"how-to-check-a-var-for-null-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-check-a-var-for-null-value-collection-of-common-programming-errors\/","title":{"rendered":"How to check a var for null value?-Collection of common programming errors"},"content":{"rendered":"<pre><code>if (result != null || result.Count() == 0) {\n    \/\/ Checks whether the entire result is null OR\n    \/\/ contains no resulting records.\n}\n<\/code><\/pre>\n<p>I think the problem is not in your check for <code>null<\/code>, because linq is lazy loading. Your error is in using the expression <code>db.SingleOrDefault(getUserQuery);<\/code>.<\/p>\n<p><code>.Single(expression)<\/code> does not return null &#8211; it errors if the result returns no values. <code>.SingleOrDefault(expression)<\/code>, however, returns a null value if the expression results in no values &#8211; and therefore is best combined with an <code>if (result == null)<\/code> type check, as you&#8217;re using here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(getUserQuery);. .Single(expression) does not return null &#8211; it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3907","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3907"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3907\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}