title: LINQ anonymous casting exception
body: <p>New to LINQ ... thought I would give it a "simple" try and get my feet wet (so to say)... I went to MS's site (101 easy linq's or something like that that) pulled an example and tried to modify it.</p>
  
  <p>Here are the classes I am working with:</p>
  
  <pre><code>public class RPt
  {
      public RPt(int n, LatLon ll, int pt){Road = n;LL = ll;Pt = pt;}
      public int Road;
      public LatLon LL;
      public int Pt;
  }
  
  public class dRpt : RPt
  {
      public dRpt(double d, RPt r) : base(r.Road, r.LL, r.Pt){D = d;}
      public double D;
  };
  
  public class LatLon
  {
      public LatLon() { Latitude = 0.0; Longitude = 0.0; }
      public LatLon(double lat, double lon){ Latitude = lat; Longitude = lon; }
      public double Latitude { get; set; }
      public double Longitude { get; set; }
  }
  
  public class RoadList : List&lt;RPt&gt;
  {
      public RoadList(){}
  
      public void FillIt()
      {
          RPt ll = null;
          ll = new RPt(1, new LatLon(37.252450, -122.058259), 10); this.Add(ll);
          ll = new RPt(1, new LatLon(37.248640, -122.068356), 20); this.Add(ll);
          ll = new RPt(1, new LatLon(37.254100, -122.089025), 30); this.Add(ll);
          ll = new RPt(1, new LatLon(37.261881, -122.102711), 40); this.Add(ll);
          ll = new RPt(1, new LatLon(37.260418, -122.112793), 50); this.Add(ll);
          ll = new RPt(1, new LatLon(37.256413, -122.116153), 60); this.Add(ll);
          ll = new RPt(1, new LatLon(37.258525, -122.121971), 70); this.Add(ll);
          ll = new RPt(2, new LatLon(37.258525, -122.121971), 10); this.Add(ll);
          ll = new RPt(2, new LatLon(37.259727, -122.124134), 20); this.Add(ll);
          ll = new RPt(2, new LatLon(37.259971, -122.129823), 30); this.Add(ll);
          ll = new RPt(2, new LatLon(37.267248, -122.138307), 40); this.Add(ll);
          ll = new RPt(2, new LatLon(37.285609, -122.152232), 50); this.Add(ll);
          ll = new RPt(2, new LatLon(37.291702, -122.156238), 60); this.Add(ll);
          ll = new RPt(2, new LatLon(37.293491, -122.161288), 70); this.Add(ll);
      }
  }
  </code></pre>
  
  <p>I am trying to filter a RoadList using a LINQ statement (like a sql join) and then have that filtered result set cast back to my RoadList instead of an anonymous 'var'.</p>
  
  <p>Here is the setup function:</p>
  
  <pre><code>    RoadList hwy;
      private void TestLinq()
      {
          hwy = new RoadList();
          hwy.FillIt();
  
          List&lt;int&gt; list = new List&lt;int&gt;() { 10 };
  
          try
          {
              RoadList filtered = LinqFilter(list, hwy);
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.ToString());
          }
  
      }
  </code></pre>
  
  <p>Here is the LINQ code: ( and the problem area )</p>
  
  <pre><code>    private RoadList LinqFilter(List&lt;int&gt; list, RoadList input)
      {
          RoadList retval = null;
  
          // this does what I want it to do...
          // returns only the list of RPt objects that match the ints
          // in the incoming list
          var q2 =
          from c in input
          join p in list on c.Pt equals p
          select new { RPt = c };
  
          retval = (RoadList)q2;  // &lt;&lt;== This is where I get the exception !
  
          foreach (RPt rpt in retval)
          {
              Debug.WriteLine(rpt.ToString());
          }
  
          return retval;
      }
  </code></pre>
  
  <p>Here is the exception I am getting:</p>
  
  <pre><code>Unable to cast object of type '&lt;JoinIterator&gt;d__61`4[deleteMe_Test.RPt,System.Int32,System.Int32,&lt;&gt;f__AnonymousType1`1[deleteMe_Test.RPt]]' to type 'deleteMe_Test.RoadList'."
  </code></pre>
  
  <p>Help... need to get back to my class object so I can return it to another function !</p>
  
  <p>Thank you.</p>