Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Linq, is DbNull equivalent to Null?

Tags:

null

tsql

linq

This is not about DBNull vs Null. I understand the difference.

What I would like to know is if I am using Linq, say to access a User.EmailAddress, then checking User.EmailAddress == null is the same as User.EmailAddress == DBNull correct?

My reasoning is that the absence of data in the database results into Linq not generating an object reference, which then means that null is in fact equivalent to DBNull when used with Linq.

Is my reasoning correct or not?

like image 717
Alex Avatar asked Jun 01 '09 20:06

Alex


People also ask

What is the difference between DBNull and null?

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

Is DBNull or empty c#?

DBNull represents a nonexistent value returned from the database. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.


2 Answers

You shouldn't use DBNull with LinqToSql. The point is Language Integration, and so one concept or name for null will suffice.

like image 51
Amy B Avatar answered Oct 07 '22 15:10

Amy B


Here's the select statement that works in LINQ to SQL for Visual Basic. I assume it will be the same in C#.

User.EmailAdress.Equals(Nothing)

For example:

Dim EmptyEmailAddressEntries = From User in DC.Users _
    Where User.EmailAddress.Equals(Nothing) select User

Will give you all the Users that have nothing in the email address. To check for entries with space " " characters only add

Or

User.EmailAddress = ""
like image 22
Michael B Avatar answered Oct 07 '22 17:10

Michael B