title: Linq - Casting IQueryable to IList returns null - WHY?
body: <p>I have to be missing something obvious here. I don't get why this cast of the results of a linq query returns null and not the typed list I'm requesting.</p>
  
  <pre><code>IList&lt;IMyDataInterface&gt; list = query.ToList() as IList&lt;IMyDataInterface&gt;;
  </code></pre>
  
  <p>The complete code to run this is below. This is a knowledge gap I need to bridge. I have tried all kinds of permutations of casts to get it to work. I get no Exceptions, just a null. Of note is that the Linq query is selecting its results into instances of my custom "MyDataClass" which implements IMyDataInterface</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          IMyFunctionalInterface myObject = new MyClass();
  
  
          //myObject.Get() returns null for some reason...
          IList&lt;IMyDataInterface&gt; list = myObject.Get();
  
          Debug.Assert(list != null, "Cast List is null");
      }
  }
  
  public interface IMyFunctionalInterface
  {
      IList&lt;IMyDataInterface&gt; Get();
  }
  
  public class MyClass : IMyFunctionalInterface
  {
      public IList&lt;IMyDataInterface&gt; Get()
      {
          string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
  
          var query = from n in names
                      where n.Contains("a")
                      select new MyDataClass
                      {
                          Name = n.ToString()
                      };
  
          //There IS data in the query result
          Debug.Assert(query != null, "result is null");
          //but the cast here makes it return null
          IList&lt;IMyDataInterface&gt; list = query.ToList() as IList&lt;IMyDataInterface&gt;;
  
          return list;
      }
  
  }
  public interface IMyDataInterface
  {
      string Name { get; set; }
  }
  
  public class MyDataClass : IMyDataInterface
  {
      public string Name { get; set; }
  }
  </code></pre>