I have below code .ToDictionary is working unexpected and throwing an error "Object reference not found".
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));
if i replace this code like.
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);
}
by using this code there is no exception but not able to figure out what is the real cause for this exception.