Adding a lambda expression led to a strange error when trying to compile

So, there is currently a piece of code that looks like this ...

string name = GetValues(sequenceOfCodes, 0, IDtoMatch, 1)[0]; 

I just updated the following line to be

 string name = sequenceOfCodes .Select(x => x[0]) .Where(x => x == IDtoMatch) .FirstOrDefault(); 

Hope we will return the same.

sequenceOfCodes is a List<List<String>> , and IDtoMatch also a string .

So, hopefully, all this seems wonderful.

However, when I go to compilation, I get an odd error

 The type 'System.Windows.Forms.ComboBox' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 

And when I take my newly added code, it compiles and runs ... So why is it just because I added lambda expression , it believes that I need a link to System.Windows.Forms.ComboBox ?

Just indicate that this application is console . Not a winforms application.

----------- UPDATE ----------

So, I found that one of the links refers to System.Windows.Forms, which I really disappoint, as this is the main code and should not have such dependencies :(

However, I am still wondering why the error did not appear before I added my line of code.

To confirm, if I delete my code, I can close VS down, restart and rebuild, and everything is fine. If I add my line of code, close and restart, etc. An error will appear during recovery.

A very strange mistake for me.

Thank you guys for your help.

+6
source share
1 answer

You mentioned that one of the other projects refers to window forms. I assume that this project also declares some extension methods that are in scope (subject to your using directives) and that the compiler should examine to resolve the overload - presumably the Where , Select or FirstOrDefault ; this means that he cannot decide that the best overload of this data is System.Linq.Enumerable until she compares it with other candidates, and she cannot do this without being able to understand the types used in the signatures of competing methods.

Or, in other words: is there a Select , Where or FirstOrDefault custom extension method that mentions a ComboBox ?

+1
source

Source: https://habr.com/ru/post/956537/