Linq查询条件里有可空字段比较时报错variable '<>h__TransparentIdentifier2' of type referenced from scope '', but it is not defined

时间:2020-11-28 16:37:16
当我运行下面的linq查询时报了这个错,
 
  1: var result = (from so in svcContext.new_sales_orderSet
  2: join soitem in svcContext.new_sales_order_itemSet on so.Id equals soitem.new_sales_orderid.Id
  3: join fpitem in svcContext.new_fp_itemSet on soitem.new_modelid.Id equals fpitem.new_modelid.Id
  4: join fp in svcContext.new_floor_priceSet on fpitem.new_fp.Id equals fp.Id
  5: where so.Id == currentEntity.Id
  6: && soitem.new_invoice_price > fpitem.new_floor_price
  7: select (soitem.new_invoice_price - fpitem.new_floor_price) * soitem.new_qty
  8: ).ToList();
 
找了半天原因,才发现是这里的原因:
&& soitem.new_invoice_price > fpitem.new_floor_price
 
因为这二个字段都是Decimal?,有些值是null,比较起来就报错了。
 
把这个条件去掉,然后再加上where条件就可以了:
var result = query.Where(q => q != null && q.Value > 0).ToList();