var dtDeviceDataSum = (from a in dtDeviceDataSameSum
join b in dtDeviceDataDiffSum
on (Convert.ToDateTime(a.currtime).Day)
equals (Convert.ToDateTime(b.currtime).Day)
into c
from o in c.DefaultIfEmpty()
select new
{
dateType = Convert.ToDateTime(a.currtime).Day,
currQty = System.DBNull.Value.Equals(a.currElectQty) ? 0 : a.currElectQty,
beforeQty = o.currElectQty.HasValue ? o.currElectQty : decimal .Zero
}).ToList();
dtDeviceDataDiffSum比少dtDeviceDataSameSum几行数据,
所以这个beforeQty是有可能为null的,我这么写按理说没错啊,
为什么一直报“未将对象引用到实例” 。
3 个解决方案
#1
非常简单:
beforeQty =(o!=null && o.currElectQty.HasValue ) ? o.currElectQty.Value : decimal .Zero
beforeQty =(o!=null && o.currElectQty.HasValue ) ? o.currElectQty.Value : decimal .Zero
#2
#3
用之前,先判断下是否为空
#1
非常简单:
beforeQty =(o!=null && o.currElectQty.HasValue ) ? o.currElectQty.Value : decimal .Zero
beforeQty =(o!=null && o.currElectQty.HasValue ) ? o.currElectQty.Value : decimal .Zero
#2
#3
用之前,先判断下是否为空