我有下面的代码.ToDictionary工作意外,并抛出一个错误“对象引用找不到”。
var serviceOptions = serviceDurations.ToDictionary(so => so.OptionCode, StringComparer.OrdinalIgnoreCase);
var serviceLines = serivceLinePayments.Select(sl => new ServiceLine(serviceOptions[sl.option_code], Decimal.ToInt32(sl.quantity), sl.customer_paid_amount));如果我像这样替换这段代码。
var serviceOptions = serviceDurations.ToDictionary(so => so.OptionCode, StringComparer.OrdinalIgnoreCase);
//var serviceLines = serivceLinePayments.Select(sl => new ServiceLine(serviceOptions[sl.option_code], Decimal.ToInt32(sl.quantity), sl.customer_paid_amount));
List<ServiceLine> serviceLines = new List<ServiceLine>();
foreach (var item in serivceLinePayments)
{
var so = serviceOptions.FirstOrDefault(s => s.Value.OptionCode == item.option_code);
ServiceLine line = new ServiceLine( so.Value, Decimal.ToInt32(item.quantity), item.customer_paid_amount);
serviceLines.Add(line);
}通过使用此代码,不存在异常,但无法找出导致此异常的真正原因。
发布于 2015-05-30 05:00:30
我相信你的代码行serviceDurations.ToDictionary( ...抛出了空引用异常,这是很明显的原因,因为serviceDurations实例是null的,因为某些原因,所以异常。
您必须调试并找出serviceDurations实例为空的原因。
https://stackoverflow.com/questions/30538934
复制相似问题