tags:

views:

1124

answers:

2

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?

+5  A: 

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

David B
A: 

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.)

jrista
This also answers a question I've been pondering but haven't asked: what's the point of L2S? It seems to me (still), that it's silly to have to learn a different syntax when just invoking SQL is so easy. But at least now I understand the theoretical reason for it....
RolandTumble
L2S is an Object-Relational Mapper. While it can be easy to just write SQL...by writing it manually (embedded in your code or in stored procs) its something else you have to manage and version on top of your code. ORM's remove the need, for the most part, to write SQL manually. The benefit of this is that if your objects or your relational schema change...the sql adapts dynamically. It can be a huge timesaver, and better allows you to meet changing business requirements and achieve greater business adaptability/agility, which what we as developers are tasked with anyway.
jrista