I have this snippet.
List<Frames> FrameList;
where Frames is a class containing only primitives including a string field “ExerciseID”.
...
void GetFramesForExercise(string exerciseID)
....
if (exerciseID == "3.2.2") {
Console.Write(""); } // quick and dirty to add a breakpoint
if (FramesList[115].ExerciseID.Equals(exerciseID)) {
Console.Write(""); } // quick and dirty to add a breakpoint
frames = (Frames)FramesList.Single(r => r.ExerciseID.Equals(exerciseID));
By putting breakpoints on the console.write statements, I am able to see that exerciseID does indeed equal “3.2.2” and that FramesList[115] points to an instance of Exercise with ID equal to “3.2.2”. The instance pointed to is correctly initiliased.
Why does my query throw an InvalidOperationException?
If there are more than one matching elements,
Singlewill throw anInvalidOperationException. (As you’ve checked there is at least one that matches, this is the only reason I can see that you’d get this exception.)See the Exceptions section of this page.