LINQ 动态表达式 API,谓词与 DBNull.Value 比较
我在使用动态表达式 API 时遇到问题。 我似乎无法将 DataTable 字段与 DBNull.Value 进行比较。 该API应该能够“支持静态字段或静态属性访问。可以访问任何公共字段或属性。”。 但是,给出以下查询:
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");
我最终收到错误:“类型 '<>f__AnonymousType0`2' 中不存在属性或字段 'DBNull'”
有人知道如何解决此问题吗? 顺便说一句,我不能在传递给Where方法的字符串中使用Submission.Field("NullableIntColumnName"),否则我将能够与null而不是DBNull.Value进行比较。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
.Where(a => a.IntColName == null);
编辑:
抱歉,我没有看到这个动态要求...动态将是:(至少在框架 4 中)
.Where(a => a.IntColName == null);
Edit:
Sorry, I did't see this dynamic requirement... Dynamic would be: (at least in Framework 4)
很抱歉没有用 USL 来回答,但是...
您查看过源代码吗? 数量并不多。 我的猜测是 DBNull 不在注册根对象的列表中。
我现在手头没有源代码,但它也可能告诉您可以比较的任何其他常量可能是什么。
Sorry to non-answer with a USL but...
Have you looked in the source? There's not a lot of it. My guess is that DBNull is not in the list of registered root objects.
I dont have the source to hand right now, but it is also likely to tell you what any other constants one might compare against might be.
一般来说,您还可以尝试:
In general, you can also try:
如果将
x.Field("Table1_ID")
更改为x.Field("Table1_ID")
那么您将得到可为 null 的整数常规整数,任何DBNull
值都将转换为简单的 C# null 值。 仅根据您的代码片段,我什至不确定您是否需要动态表达式 - 一个简单的.Where(foo => foo.x == null)
应该可以工作。If you change
x.Field<int>("Table1_ID")
tox.Field<int?>("Table1_ID")
then you'll get nullable integers instead of regular integers, and anyDBNull
values will be converted to simple C# null values. Based simply on your code snippet, I'm not even sure you'd need dynamic expressions - a simple.Where(foo => foo.x == null)
ought to work.当你用类似的东西替换当前的 .Where 时会发生什么
What happens when you replace your current .Where with something like
好吧,我终于明白了。 cptScarlet 几乎已经做到了。
或者
Well, I finally got it. cptScarlet almost had it.
or