title: c# DataTable - working out percentages, even for records which don't exist
body: <p>I hope folks can help me out with a problem I'm trying to solve. It's really baffled me:</p>
  
  <p>I have a C# datatable, with for example, the following content:</p>
  
  <pre><code>LotNo   Level   IsValid?
  123         2   Yes
  123         2   Yes
  123         2   Yes
  123         3   Yes
  123         3   No
  123         3   Yes
  456         2   Yes
  456         2   No
  456         2   No
  456         2   No
  456         3   Yes
  890         1   No
  890         1   No
  </code></pre>
  
  <p>The "<strong>IsValid?</strong>" column will only ever be "<strong>Yes</strong>" or "<strong>No</strong>". Whereas the <strong>LotNo</strong> and <strong>Level</strong> columns are always dynamic. Also the records aren't in any order (unlike the table above).</p>
  
  <p>I'm trying to figure out the code to evaluate the percentage of records which have "<strong>IsValid = Yes</strong>" across the total of all records for that LotNo and Level.</p>
  
  <p>So in the example above, I'm looking for the following output:</p>
  
  <pre><code>**LotNo Level   PctValid?** 
  123     2       100     [ie 3 x Yes out of a total of 3 items for LotNo 123 and Level 2]
  123     3       66.66   [ie 2 x Yes out of a total of 3 items for LotNo 123 and Level 3]
  456     2       25      [ie 1 x Yes out of a total of 4 items for LotNo 123 and Level 2]
  456     3       100     [ie 1 x Yes out of a total of 1 item for LotNo 456 and Level 3]
  890     1       0       [ie zero x Yes out of a total of 2 items for LotNo 890 and Level 1]
  </code></pre>
  
  <p>What I'm finding particularly difficult is coping with the instance where there are <em>no</em> records - for example, LotNo=890 and Level=1 has no records where "IsValid=Yes". I know I sort of need a kind SQL "outer-join" effect. I also need some kind of SQL group by - not to sure how this applies with a DataTable.</p>
  
  <p>I've tried breaking it down into "for" loops, and creating a new records when the loop encounters a new batch number, but it quickly descends into madness trying to cope with the dynamic number of levels.</p>
  
  <p>I hope I've phrased this question okay - I'm not a regular poster.</p>
  
  <p>Any pointers on where on earth to start? I'm baffled!</p>