{"id":543,"date":"2012-09-27T14:14:26","date_gmt":"2012-09-27T14:14:26","guid":{"rendered":"http:\/\/foxware.co.uk\/?p=543"},"modified":"2012-09-27T15:41:52","modified_gmt":"2012-09-27T15:41:52","slug":"linq-queries-handling-nulls","status":"publish","type":"post","link":"http:\/\/foxware.co.uk\/?p=543","title":{"rendered":"LINQ Queries: handling nulls"},"content":{"rendered":"<p>Quite often when querying databases using data from text boxes, you can run into problems dealing with a blank string and fields containing a null.<\/p>\n<p>A field in a table may contain a null which could also mean a blank field but you can run into problems when you are trying to query a condition on the field.<\/p>\n<p>This example solved the problem;<\/p>\n<pre class=\"lang:c# decode:true \" >var DupQuery = (from recs in db.tAXModelKitXref where (txtProdCode.Text == \"\" ?  recs.ProdCode.Equals(null) : recs.ProdCode.Equals(txtProdCode.Text)) &amp;&amp; \r\n                                    (txtKitCode.Text == \"\" ?  recs.KitCode.Equals(null) : recs.KitCode.Equals(txtKitCode.Text)) &amp;&amp; \r\n                                    recs.UniqueId != Unique select new { id = recs.UniqueId }).ToList();<\/pre>\n<p>All i&#8217;m doing in the LINQ query is checking if the textbox (a string) is blank, if it is then I pass a null to condition.<\/p>\n<p>If you are planning to use a null when the field is blank, then also when you are saving data to the field you need to use the followin example;<\/p>\n<pre>Record.ProdCode = (txtProdCode.Text == \"\" ? null : txtProdCode.Text);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quite often when querying databases using data from text boxes, you can run into problems dealing with a blank string and fields containing a null. A field in a table may contain a null which could also mean a blank &hellip; <a href=\"http:\/\/foxware.co.uk\/?p=543\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/543"}],"collection":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=543"}],"version-history":[{"count":9,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":545,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions\/545"}],"wp:attachment":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}