I have the following expression, where a.AnswerId is of type long?. ReSharper warns of a possible InvalidOperationException in the select function. Is there ever a case where this could actually happen? (corner-cases are fine too)
long[] ids = answers.Where(a => a.AnswerId.HasValue)
.Select(a => a.AnswerId.Value)
.ToArray();
Since you check in the
Wherethata.AnswerIdhas a value,a.AnswerId.Valuewill never throw anInvalidOperationException(unless another thread is changing the data at the same time). Resharper has pretty good code analysis capabilities, but it can’t spot everything, and in this case it doesn’t realize that theWheremakes it safe to call.Valuein theSelect, hence the warning. So you can safely ignore this warning.