title: Using an anonymous type in a try catch c#
body: <p>I have struggled with this for years and normally just code my way around it, but it is time to solve it.</p>
  
  <p>I am declaring a var that returns a new anon type and want to put it in a try/catch.
  However, doing that means it is out of scope and cannot be seen by later code obviously.
  Normally I just declare it first, then wrap the code in the try/catch then reassign inside of it like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>int result = 0;
  
  try
  {
      result = 77; //real code goes here
  }
  catch (Exception)
  {
      throw;
  }
  </code></pre>
  
  <p>But here is my real code that I cannot figure out how to do something like that:</p>
  
  <pre class="lang-cs prettyprint-override"><code>
      try
      {
          var dt_stop = (from s in cDb.DistributionStopInformations
                         join r in cDb.DistributionRouteHeaders on s.RouteCode equals r.RouteCode
                         where r.RouteDate == s.RouteDate &amp;&amp;
                         r.BranchId == s.BranchId &amp;&amp;
                         (r.CompanyNo == companyNo &amp;&amp; s.CompanyNo == companyNo)
                         &amp;&amp; s.UniqueIdNo == uniqueId
  
                         select new
                         {
                             s,
                             r
                         }).Single();
      }
      catch (Exception)
      { //no this will not be blank
          throw;
      }
  </code></pre>
  
  <p>UPDATE:
  I do use dt_stop extensively after this, I am wanting to catch if there is a problem with it is assigned data.</p>
  
  <p>I created the following class:</p>
  
  <pre><code> public class StopData
  {
      public DistributionStopInformation S { get; set; }
  
      public DistributionRouteHeader R { get; set; }
  }
  </code></pre>
  
  <p>Then I attempt to use is like:</p>
  
  <pre><code> StopData dt_stop = null;
  
          try
          {
              dt_stop = (from S in cDb.DistributionStopInformations
                         join R in cDb.DistributionRouteHeaders on S.RouteCode equals R.RouteCode
                         where R.RouteDate == S.RouteDate &amp;&amp;
                         R.BranchId == S.BranchId &amp;&amp;
                         (R.CompanyNo == companyNo &amp;&amp; S.CompanyNo == companyNo)
                         &amp;&amp; S.UniqueIdNo == uniqueId
  
                         select new StopData
                         {
                             S,
                             R
                         }).Single();
  
          }
          catch (Exception)
          {
              //YES....THERE WILL BE CODE HERE
              throw;
          }
  </code></pre>
  
  <p>I am getting
      Cannot initialize type 'StopData' with a collection initializer because it does not implement 'System.Collections.IEnumerable'  </p>