LINQ Dynamic Expression API, predicate with DBNull.Value matching

I have a problem using the dynamic expression API. I cannot compare the DataTable field with DBNull.Value. It is assumed that the API will be able to "support access to a static field or static property." Access to any open field or property. "However, given the following request:

 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 get the error: "No property or field" DBNull "exists in type" <> f__AnonymousType0`2 ""

Anyone have any ideas on how to get around this? I cannot use Submission.Field ("NullableIntColumnName") in the string passed to the Where method, either btw, or else I could compare with null instead of DBNull.Value.

+3
source share
6 answers

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

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

or

.Where("x[\"NullableIntColumnName\"] == @0", DBNull.Value);
+3
source

What happens when you replace your current one. Somewhere like

.Where(string.format("x[\"NullableIntColumnName\"] == {0}",DBNull.Value));
+1
source

x.Field<int>("Table1_ID") x.Field<int?>("Table1_ID"), , DBNull # null. , , - .Where(foo => foo.x == null) .

+1

, :

.Where("NullableColumnName.HasValue");
+1

- USL, ...

? . , DBNull .

I don't have a source to feed right now, but it will also probably tell you what other comparable constants might be.

0
source

. Where (a => a.IntColName == null);

Edit:

Sorry, I did not see this dynamic requirement ... Dynamic will be: (at least in Framework 4)

    var intColName = "...";
    .Where(string.Format("it.{0} is null", intColName));
0
source

Source: https://habr.com/ru/post/1713454/


All Articles