1

I have to join 2 different datatable like with linq:

   // let use Linq
        var DateMarket = from p in IndexPrice.AsEnumerable()
                         join q in TickerPrice.AsEnumerable() on p.Field<DateTime>("DATE") equals q.Field<DateTime>("DATE") into UP
                         from q in UP.DefaultIfEmpty()
                         where p.Field<DateTime>("DATE") != null && !q.IsNull("CHG_PCT_1D")
                         select TestRecap.Rows.Add(p.Field<DateTime>("DATE"), q.Field<Double>("CHG_PCT_1D")) ;

however even if I use the condition :

  where p.Field<DateTime>("DATE") != null && !q.IsNull("CHG_PCT_1D")

I still have a NullReferenceException at this line. Do you have an idea why ?

Thanks

4

2 回答 2

1

from q in UP.DefaultIfEmpty()表示,如果没有q为您的 找到匹配项p,它将使用默认值,即 null(与 withFirstOrDefault()SingleOrDefault()函数相同)。

检查q != null,它应该可以工作。

于 2012-01-10T07:58:49.367 回答
0

p 和 q 中的一个对于加入后的行可能为空。检查他们的无效。

where p!=null 
   && p.Field<DateTime>("DATE") != null 
   && q != null 
   && !q.IsNull("CHG_PCT_1D")
于 2012-01-10T07:54:16.267 回答