Reputation: 23
Never used LINQ
within C# before until 30 mins ago and am struggling to find an answer to my query online (probably due to my lack of understanding).
I have a simple query
var variableName = from a in tableName.AsEnumerable()
where a.column1 == item1
&& a.column2 == item2
&& a.column3 != null
select a;
The SQL column is defined as an int, null.
When the code encounters a record that is null on the database for column3
, the following error is generated "the value for column3 in table <tableName> is DBNull
".
Instead of checking for != null
, i guess i should be using something else, but have attempted checking for DBNull
.Value but the compiler states "Operation != cannot be applied to operands of type int and system.DBNull
".
Any ideas?
Upvotes: 2
Views: 2730
Reputation: 2882
try
var variableName = from a in tableName.AsEnumerable()
where a.column1 == item1
&& a.column2 == item2
&& !DBNull.Value.Equals(a.column3)
select a;
edit apparently I need to read up on typed data sets :) and why I should never use them
Upvotes: 0
Reputation: 1062502
This looks like a typed dataset, which : yeuch - stop using those, but I digress.
As such, accessing a.column3
will always raise an exception if that value is DBNull
. You would need to use the typed-dataset pattern:
&& !c.Iscolumn3Null()
Upvotes: 6
Reputation: 5
Try this..
var variableName = from a in tableName.AsEnumerable()
where a.column1 == item1
&& a.column2 == item2
&& a.column3 != dbnull.value
select a;
Upvotes: 0
Reputation: 125610
tableName.AsEnumerable()
makes the query in-memory, so all table rows are downloaded from DB and the conditions are checked on application.
Try that:
var variableName = from a in tableName
where a.column1 == item1
&& a.column2 == item2
&& a.column3 != null
select a;
It should be translated into an SQL query and download only necessary rows.
Upvotes: 0