我试图在LINQ中为两个var
s做左外连接但是在选择所需的颜色时,我得到Object reference not set to an instance of an object
错误我想要Nullable decimal
。
var FLS = (from ee in SumTillFYEnd
join es in SumTillFYStart on ee.Account equals es.Account into temp
from t in temp.DefaultIfEmpty()
select new
{
Account = ee.Account, // As of here it works
BeginDr = (t.DrStartCF == 0) ? (decimal?) null : t.DrStartCF // Here I get error Object reference not set to an instance of an object.
});
有时SumTillFYEnd和有时SumTillFYStart变为null。我想加入应该使用默认值,以防任何一个或两个为null。
问题是试图将null
投射到decimal?
。您不能直接将null
转换为其他类型,可以为空或无空。这将永远导致NullReferenceException
。你想要的是default
。换句话说,替换:
(decimal?)null
同
default(decimal?)