title: LINQ lambda expression throws Sequence contains no elements
body: <p>I know what this error means, but I am not sure how to fix it in my LINQ statements.</p>
  
  <p>ScannerMessages is a dictionary . The values in this will either be numeric from a serial scan, or string.empty, or (before any scans happen) null. I have a feeling someone will say to use FirstorDefault(), but I can't use the default value of null/string.empty. I tried using</p>
  
  <pre><code>.Any(x =&gt; !string.IsNullOrEmpty(x.Value)). 
  </code></pre>
  
  <p>but this returns a bool and is the wrong check. I feel like I need a Where() but I already tried that too to no avail (I also tried changing my GroupBy() clauses). I feel like I am close but I am not sure where to modify my expressions.</p>
  
  <p>Note: This is inside a lock so I know none of my other threads are affecting the dictionary while I am querying against it (ScannerMessages is locked)</p>
  
  <pre><code>    public ScanType equalMessages()
      {
          try
          {
              lock (lckObj)
              {
                  if (_scannersPresent)
                  {
                      // Check to see if message that came through was caught in the buffer. If it has it will be equal to the previous message.
                      if (ScannerMessages.GroupBy(x =&gt; x.Value).Where(x =&gt; x.Count() &gt; 1).First().Key == _holdMsg) return ScanType.ClearBuffer;
                      // If all messages are null or empty no scan
                      else if (ScannerMessages.Values.All(s =&gt; string.IsNullOrEmpty(s))) return ScanType.NoScan;
                      // If there is only one scanner then all scans must be determined to be either MATNUM or a rescan of an LPN
                      else if (_ScannerCount == 1) return ScanType.DiscernScanType;
                      // If distinct count of non null/empty values is less than total count of all non null/empty values, then scan is good
                      else if (ScannerMessages.Values.Distinct().Count(v =&gt; !string.IsNullOrEmpty(v)) &lt; ScannerMessages.Values.Count(v =&gt; !string.IsNullOrEmpty(v)))
                      {
                          // This condition is only met if there is more than one of the same scan, and therefore is not a reprint. Pad all values with '0's for proper MATNUM format
                          ScannerMessages = ScannerMessages.Where(x =&gt; !string.IsNullOrEmpty(x.Value)).ToDictionary(x =&gt; x.Key, x =&gt; x.Value.PadLeft(18, '0'));
                          return ScanType.GoodScan;
                      }
                      // If non null/empty counts is equal to one, and the message is not the same as previous scan, then message was not from buffer and is LPN for reprint
                      else if ((ScannerMessages.Values.Distinct().Count(v =&gt; !string.IsNullOrEmpty(v)) == 1) &amp;&amp; (ScannerMessages.GroupBy(x =&gt; x.Value).Where(x =&gt; x.Count() &gt; 1).First().Key != _holdMsg)) return ScanType.Reprint;
                      else return ScanType.NoScan;
                  }
                  return ScanType.NoScan;
              }
          }
          catch (Exception ex) {
              myEvents.raiseErrorMessageBox(ex.Message);
              throw new Exception(ex.Message, ex); }
      }
  </code></pre>
  
  <p>Edit: I am not sure which line is throwing the error- I think it is either the first if line <em>or</em> the final else if before the else return ScanType.NoScan. Those are the only statements using the First() call. The problem is this gets called about every half second so debugging it backs up. There are 3 threads accessing the ScannerMessages dictionary, and then the thread that calls this checking that dictionary. I am not sure how to debug it when I have 4 threads hitting it. The only thing I know is that it is* thread-safe :/ If I try to put a breakpoint on the first line, it hits so many times that while I'm stepping through I miss any "real" scans.</p>