title: Delete record and conditionally delete others LINQ
body: <p>Let's say I have the following classes in <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query#LINQ_to_SQL" rel="nofollow">LINQ&nbsp;to&nbsp;SQL</a>:</p>
  
  <pre><code>Module
  {
      long MID    // PK
      string Name
  }
  
  ModuleBlock
  {
      long MID      // FK
      long BID      // FK
  }
  
  Block
  {
      long BID      // PK
      string Info
  }
  
  BlockLanguage
  {
      long BID      // FK
      long LID      // FK
  }
  
  Language
  {
      long LID      // FK
      string Language
  }
  </code></pre>
  
  <p>Now, I'd like to delete a set of moduleBlocks</p>
  
  <pre><code>var toBeDeletedModuleBlocks = // Query, select ModuleBlocks to be deleted
  </code></pre>
  
  <p>The query for toBeDeletedModuleBlocks is could be arbitrary, as long as it selects ModuleBlocks.</p>
  
  <p>For each Block that has will have 0 of its foreign keys (BID) inside ModuleBlock AFTER ModuleBlocks have been deleted, I'd like to delete that Block.</p>
  
  <p>So basically if there exists a BID in Blocks that does not exist in ModuleBlock, I'd like to delete that Block.</p>
  
  <p>And delete every BlockLanguage with the FK(BID) that is the PK(BID) of the Blocks that are to be deleted.</p>
  
  <p>And this has to happen with <strong><em>one</em></strong> submitChanges on the datacontext. I've been working on this for an hour, but I simply lack the <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="nofollow">LINQ</a> knowledge to query everything I need to delete.</p>
  
  <p>In the end I'd like to be able to do this:</p>
  
  <pre><code>var toBeDeletedModuleBlocks =
      from moduleBlocks in context.ModuleBlocks
      where block.MID == 5 // assume block with MID = 5 exists
      select moduleBlocks
  
  // Count of each block referenced by ModuleStrings
  var allBlocksCount = 
     from moduleBlock in context.ModuleBlocks
     join block in context.Blocks on ModuleBlock.bID equals block.Bid
     group by block by block.BID into counter
     select new
     {
        BID = counter.Key,
        Count = counter.Count()
     };
  
  // Count of each block that is going to be deleted, in this case it has a maximum of 1 (one module in each where).
  var theseBlocksCount =
     from moduleBlock in context.ModuleBlocks
     join block in context.Blocks on ModuleBlock.bID equals block.Bid
     where moduleStrings.MID == 5 // 5 again, these are going to be deleted
     groub by 
     select new
     {
        BID = count.Key
        Count = counter.Count()
     }
  </code></pre>
  
  <p>Now I'd like to compare allBlocksCount &amp; theseBlockscount, if Count equals each other that means that there will be 0 references left in BlockString, so I'd like a query that gives me all the Blocks (BIDs) where count in allBlocksCount &amp;&amp; theseBlocksCount is equal.</p>
  
  <p>But I don't know enough LINQ to make that query. If I knew how to do that query I could fill <strong>toBeDeletedBlocks</strong> and <strong>toBeDeletedBlockLanguages</strong></p>
  
  <p>And then I could do this.</p>
  
  <pre><code>Context.ModuleBlocks.DeleteAllOnSubmit(toBeDeletedModuleBlocks);
  Context.Blocks.DeleteAllOnSubmit(toBeDeletedBlocks);
  Context.BlockLanguages.DeleteAllOnSubmit(toBeDeletedBlockLanguages);
  Context.SubmitChanges();
  </code></pre>