Note that there are some explanatory texts on larger screens.

plurals
  1. POLambda expression oddity
    text
    copied!<p>Long story short. I have 2 lists which contain the same type (but are used for different things) and I want to know if EITHER list contains an item with a certain name.</p> <p>My original code, which worked prefectly, was:</p> <pre><code>if (listA.Any(var =&gt; var.Name == strMatch) || listB.Any(var =&gt; var.Name == strMatch)) { //Do something } </code></pre> <p>This code worked perfectly, whether the item was present in either or both lists. Later on, I had several 'impossible' crashes -- Things which never could have happened. I traced it back to that if statement NEVER returning true.</p> <p>This stumped me for ages... I couldn't work out what was going wrong. Eventually I gave up and stuck brackets around the body of the lamda expressions like so...</p> <pre><code>if (listA.Any(var =&gt; (var.Name == strMatch)) || listB.Any(var =&gt; (var.Name == strMatch))) { //Do something } </code></pre> <p>After re-running my program, all the 'impossible' errors went away and it functioned normally. Removing the extra backets cause the errors to reappear.</p> <p>I've never had this problem with lambda expressions before (especially where they work and THEN, after several runs working correctly break) and my other lambda expressions work correctly.</p> <p>Example: The following code works 100% as expected (Assuming that there is a match in one of the lists)</p> <pre><code>Item item = ListA.FirstOrDefault(var =&gt; var.Name == strMatch) ?? ListB.FirstOrDefault(var =&gt; var.Name == strMatch); </code></pre> <p>What's going on? Why is the compiler picky about <strong>some</strong> lamda expressions and not others? (Even when they are identical?) ???</p> <p><strong>UPDATE :: System Details</strong> This was encountered with Microsoft Visual Studio 2008 (Professional), Windows Vista 32bit.</p> <p><strong>UPDATE</strong> <a href="http://www.youtube.com/watch?v=VVROKeNGa-g" rel="nofollow">Video link</a>, this has been test on other computers and is <strong>NOT</strong> reproducable. Makes me feel like my computer is doomed. Reinstallion of VS has no effect.</p> <p>Please ignore any background cat noises, she only meows alot when she hears me recording something.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload