title: How To Determine Which WHERE Clause Was True In A Linq Statement
body: <p>Is there a way in LINQ to tell which WHERE clause(s) hit?  I have an object called Company, it can in turn have multiple BillTo objects, each BillTo object can have multiple Generator objects.</p>
  
  <pre><code>Company
  |
  +-- BillTo1 - named First
  |    |
  |    |-- Generator1 - named Alpha
  |    |
  |    +-- Generator2 - named Beta
  |
  +-- BillTo2 - named Second
       |
       +-- Generator3 - name Gamma
  </code></pre>
  
  <p>Here is the linq statement (stripped out extra stuff for other where statements):</p>
  
  <pre><code>bool HaveCompany = !string.IsNullOrWhiteSpace(Company);
  var AllData = (from co in db.Companies
               join bt in db.Billtoes on co.ID equals bt.CompanyID into bts
               from b in bts.DefaultIfEmpty()
               join gn in db.Generators on b.ID equals gn.BillToID into gns
               from g in gns.DefaultIfEmpty()
               where co.Active == true
               &amp;&amp; (
                  co.Name.Contains(HaveCompany ? Company : co.Name) ||
                  b.Name.Contains(HaveCompany ? Company : b.Name) ||
                  g.Name.Contains(HaveCompany ? Company : g.Name)
               )
               select new {
                   CompanyID = co.ID,
                   BillTo = b,
                   Generator = g,
                   Name = co.Name,
               }).ToList();
  </code></pre>
  
  <p>The problem is, it finds everything correctly.  If I find the "Name" match on the BillTo, I don't really need to do "Generator = g" nor vice-versa if I find it on the Generator I don't need to do "BillTo = b".</p>
  
  <p>So if ther was a way to know which WHERE clause(s) was hit, then I would add the appropriate b or g to the result.  I know I could reiterate through AFTER I get the results and re-check each of the 7 fields I am actually sending in as WHERE clauses, but that seems like too much extra code.  Perhaps a tweak of my LINQ is in order?  Are there values I can extract out via reflection or somethng to find out what was hit?</p>