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
A lambda expression cannot throw any checked exception until its corresponding functional interface declares a throws clause. An exception thrown by any lambda expression can be of the same type or sub-type of the exception declared in the throws clause of its functional interface.
Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
Hence lambda expressions enable us to write functional code. Readable and concise code: People have started using lambda expressions and reported that it can help to remove a huge number of lines from their code.
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With