简体   繁体   中英

Adding a lambda expression has resulted in a strange error when attempting to compile

So currently there is a piece of code which 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();

Which should hopefully return the same thing.

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

So hopefully this all seems fine.

However when I go to compile 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 away it compiles fine and runs... So why is it just because I have added a lambda expression does it think that I it needs a reference to System.Windows.Forms.ComboBox ?

Just to state that this is a console application. Not a winforms application.

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

Ok, So I have found that a one of the references does reference the System.Windows.Forms, which I is really disappointing as this is core code and should not have dependencies like this :(

However I still wonder why the error did not appear before until after I added my line of code.

To confirm, If I remove my code I can close VS down and restart and rebuild and all is fine. If I add my line of code and close down and restart, etc. The error will reappear on rebuild.

Very strange error to me.

Thanks guys for all your help

You mention that one of the other projects does reference windows forms. My guess is that this project also declares some extension methods that are in scope (given your using directives), and which the compiler needs to explore for overload resolution - presumably of the Where , Select or FirstOrDefault methods; meaning: it can't decide that the best overload of these is the System.Linq.Enumerable one until it has compared it to the other candidates, and it can't do that without being able to understand the types used in the competing method signatures.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM