LINQ Dynamic Expression API, predicate with DBNull.Value comparison

datatable dynamic dynamic-linq expression linq

Question

I have an issue using the Dynamic Expression API. I cannot seem to compare a DataTable field against DBNull.Value. The API is supposed to be able to "support static field or static property access. Any public field or property can be accessed.". However given the following query:

 var whatever = table1.AsEnumerable()
                   .Join(table2.AsEnumerable(),
                   (x) => x.Field<int>("Table1_ID"),
                   (y) => y.Field<int>("Table2_ID"),
                   (x, y) => new { x, y})
                   .AsQueryable()
                   .Where("x[\"NullableIntColumnName\"] == DBNull.Value");

I end up getting the error: "No property or field 'DBNull' exists in type '<>f__AnonymousType0`2'"

Anyone have ideas on how to get around this? I can't use Submission.Field("NullableIntColumnName") in the string passed to the Where method either, btw, or else I would be able to compare against null instead of DBNull.Value.

1
3
7/24/2009 2:49:34 PM

Accepted Answer

Well, I finally got it. cptScarlet almost had it.

var values = new object[] { DBNull.Value };    
...
.Where("x[\"NullableIntColumnName\"] == @0", values);

or

.Where("x[\"NullableIntColumnName\"] == @0", DBNull.Value);
3
7/24/2009 2:59:18 PM

Popular Answer

In general, you can also try:

.Where("NullableColumnName.HasValue");


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow