4

When debugging a C# project in Visual Studio 15.8.7, I want to use Linq methods like Single() or FirstOrDefault() in the Immediate Window like this:

allGroups.Single(ag => ag.ImportId == importId)

However, I sometimes get an error message telling me

Evaluation of method System.Linq.Enumerable.FirstOrDefault() calls into native method System.Environment.FailFast(). Evaluation of native methods in this context is not supported.

I cannot always reproduce this error, for some values of importId it shows me the group I'm interested in, for some other values, it throws the error.

I have enabled Use managed compatibility mode in the debugger options. What else am I missing?

Thaoden
  • 3,460
  • 3
  • 33
  • 45
  • 2
    Where do you call run that code? Constructor? Form_Loaded Event? Any other Event? Generally you should not try to run code before Form_Loaded, as the Window is not guaranteed to have been built by then. The exception explicitly mentioned "Context", yet you did not give us any information about the Context this is executed in. – Christopher Oct 19 '18 at 13:38
  • 1
    This line of code is executed in the Immediate Window while debugging, I have clarified it in the question. – Thaoden Oct 19 '18 at 13:50

0 Answers0