my database field int_ParentId consist of a Null value . how would i check it for a null in this linq query . it is not working
return _db.Categories.Where(m => m.int_ParentId==null);
In SQL Server, a SQL statement like 'NULL=NULL' evaluates to false. however 'NULL IS NULL' evaluates to true. So, for NULL values in your database columns, you need to use the 'IS' operator instead of the regular '=' operator.
in conclusion no, it won't return null since null can't say sequence contains no elements it will always say object reference not set to an instance of an object ;) Oh, your explanation helps further understanding. Thank you !
An object collection such as an IEnumerable<T> can contain elements whose value is null. If a source collection is null or contains an element whose value is null , and your query doesn't handle null values, a NullReferenceException will be thrown when you execute the query. var query1 = from c in categories where c !=
Have you mapped you int_ParentId
database field to int?
type (e.g. <Column Name="int_ParentId" Type="System.Int32" DbType="Int" CanBeNull="true" />
)? If yes, both:
return _db.Categories.Where(m => m.int_ParentId == null);
and
return _db.Categories.Where(m => m.int_ParentId.HasValue);
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With