Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 123029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:13:19+00:00 2026-05-11T04:13:19+00:00

I have to be missing something obvious here. I don’t get why this cast

  • 0

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.

IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>; 

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

class Program {     static void Main(string[] args)     {         IMyFunctionalInterface myObject = new MyClass();           //myObject.Get() returns null for some reason...         IList<IMyDataInterface> list = myObject.Get();          Debug.Assert(list != null, 'Cast List is null');     } }  public interface IMyFunctionalInterface {     IList<IMyDataInterface> Get(); }  public class MyClass : IMyFunctionalInterface {     public IList<IMyDataInterface> 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<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>;          return list;     }  } public interface IMyDataInterface {     string Name { get; set; } }  public class MyDataClass : IMyDataInterface {     public string Name { get; set; } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:13:20+00:00Added an answer on May 11, 2026 at 4:13 am

    The problem here is one of covariance.

    First, your example is a bit too complicated. I have removed some fluff. Also, I’ve added some diagnostics that illuminate the problem.

    class Program {     static void Main(string[] args)     {         var names = new[] { 'Tom', 'Dick', 'Harry', 'Mary', 'Jay' };          var query = from n in names                     select new C                     {                         S = n                     };          //There IS data in the query result         Debug.Assert(query != null, 'result is null');          //but the conversion here makes it return null         var list = query.ToList() as IList<I>;         Console.WriteLine(query.ToList().GetType());          // this assert fires.         Debug.Assert(list != null, 'Cast List is null');     } }  interface I {     string S { get; set; } }  class C : I {     public string S { get; set; } } 

    The output of this program is:

     System.Collections.Generic.List`1[C] 

    Note that we’re trying to cast a List<C> to List<I> which doesn’t work in C# 3.0.

    In C# 4.0 you should be able to do this, thanks to the new co- and contra-variance of type parameters on generic interfaces.

    Also, your original question asked about IQueryable but that’s not relevant here: the query expression you supplied creates an IEnumerable<string> not an IQueryable<string>.

    EDIT: I want to point out that your ‘cast’ using the as operator is technically not a cast, but is a ‘type conversion’. If you had use a cast, you would have gotten an exception with useful information. If I change to:

        var list = (IList<I>)query.ToList(); 

    I get an InvalidCastException with:

     Additional information: Unable to cast object of type ‘System.Collections.Generic.List1[C]' to type 'System.Collections.Generic.IList1[I]’. 

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can add your tab initialization code to the Load… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer Check out this.. http://msdn.microsoft.com/en-us/library/ms537361(VS.85).aspx This explains everything you need to… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer To check for Null in a dataset field: If IsNull(rs("colname"))… May 12, 2026 at 12:53 am

Related Questions

I have to be missing something obvious here. I don't get why this cast
I'd like to store a set of key/value pairs in the application settings of
I'm probably missing something very obvious here. I have files with the extension .st
I'm trying to create a Regex usuable in C# that will allow me to
I came across an interesting problem today. I have a text email I'm sending

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.