title: Debugging LINQ Queries
body: <p>We've been doing a lot of work with LINQ lately, mainly in a LINQ-to-Objects sense.  Unfortunately, some of our queries can be a little complicated, especially when they start to involve multiple sequences in combinations.  It can be hard to tell exactly what's going on, when you get queries that start to look like:</p>
  
  <pre><code>IEnumerable&lt;LongType&gt; myCompanies =       relevantBusiness.Children_Companies
              .Select(ca =&gt; ca.PR_ContractItemId)
              .Distinct()
              .Select(id =&gt; new ContractedItem(id))
              .Select(ci =&gt; ci.PR_ContractPcrId)
              .Distinct()
              .Select(id =&gt; new ContractedProdCompReg(id))
              .Select(cpcr =&gt; cpcr.PR_CompanyId)
              .Distinct();
  
  var currentNewItems = myCompanies 
                  .Where(currentCompanyId =&gt; !currentLic.Children_Appointments.Select(app =&gt; app.PR_CompanyId).Any(item =&gt; item == currentCompanyId))
                  .Select(currentId =&gt; new AppointmentStub(currentLic, currentId))
                  .Where(currentStub=&gt;!existingItems.Any(existing=&gt;existing.IsMatch(currentStub)));
  
  
  Items = existingItems.Union(newItems).ToList();
  </code></pre>
  
  <p>etc., etc...</p>
  
  <p>Even when you debug, it can be difficult to tell who's doing what to who and when.  Short of gratuitously calling "ToList" on sequences to get things I can examine more easily, does anyone have any good suggestions for how to debug "complicated" LINQ?</p>