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?
You shouldn't use DBNull with LinqToSql. The point is Language Integration, and so one concept or name for null
will suffice.
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 = ""
In LINQ to SQL, you should be using null rather than DBNull. LINQ to SQL is an OR mapper, so it works with objects in a native way. The whole goal with L2S is to allow you to work with objects in a standard .NET way, and let L2S handle all the mapping between native and DB specific for you. You should avoid using DBNull in any L2S statements...in fact, I'm not even sure that is even a valid check (it'll probably cause some odd behavior if it works at all.)