【发布时间】:2016-02-17 09:25:00
【问题描述】:
所以我遇到了一个奇怪的问题。当我对数据表进行 linq 查询时,我得到了
“对象引用未设置为对象的实例。”
但是当对结果使用 forloop 时,它可以正常工作。dt 是一个数据表
var productdata = from data in dt.AsEnumerable()
where data.Field<string>("Edited").ToString().ToUpper() == "NEW"
select data;//I get the object reference error here
foreach (var item in productdata) //but here the control goes inside the foreachloop even though the object refrence was null and the code gets executed correctly
{
//operation
}
只有在最后一次迭代之后才会再次给出空引用异常。我不明白为什么会这样
【问题讨论】:
-
我想知道为什么forrloop得到正确执行
-
好吧,我想了这么多,那么为什么在最后一次迭代后它会给出“对象引用未设置为对象的实例”错误
-
将您的 linq 转换为另一个 foreach 循环并调试所有步骤。