Lets consider a table with 2 columns: ID (int) and Role(string). Both are nullable.
Now assume that the data in the two columns is:
ID Role
-- ----
1 NULL
2 Admin
The query looks like this:
List<types> t1 = (
from a in datacontext.RoleTable
where a.Role != "Admin"
select a
).ToList();
I thought the above query should be returning the first record of the table as its Role column is not equal to 'Admin' but the query returns an empty list.
Now when I use this query:
List<types> t2 = (
from a in datacontext.RoleType
where a.Role != "Admin" && a.Role == DBNull.Value.ToString()
select a
).ToList();
I get the correct answer.
Can anybody tell me why the first query is not working please.
FYI: If the Role column in the first row in the table is changed to User
instead of NULL
then the first query works fine.
I am using SQL Express and LINQ to SQL.