System.InvalidOperationException: Sequence contains no elements

foreach (var keyCode in unexpectedKeyCodesDetected)
{
  string unexpected = expectedCapturedKeyCodes.Where(key => key == keyCode).First();
}

 

空集合时,从First()处 Throw the exceptoins. 

修改:  First() => FirstOrDefault(),然后对返回String做null判断。

你可能感兴趣的:(exception)