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 6841741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:00:15+00:00 2026-05-27T00:00:15+00:00

For brevity, I will be generic here. I have a base class – Review

  • 0

For brevity, I will be generic here. I have a base class – Review – that has a child – ShelfAwarenessReview.

I also have a method, whose signature is required by an interface:

public List<Review> GetReviews(string filePath)
        {
            XElement xmlDoc = XElement.Load(filePath);

            var dtos = from item in xmlDoc.Descendants("message")
                       select new ShelfAwarenessReview()
                       {
                           PubDate = item.Element("meta").Attribute("permlinkdate").Value,
                           Summary = item.Element("meta").Element("summary").Value,
                           Isbn = item.Element("BookInfo").Element("ISBN").Value
                       };

            List<Review> reviews = new List<Review>();
            reviews = dtos.ToList();

            return reviews;
        }

Now, the error I am getting is that List<ShelfAwarenessReview> cannot be cast implicitly to List<Review>.

I have tried several types of casting – or, at least I thought I did – and it doesn’t work. I thought because ShelfAwarenessReview is a child of Review that this would work. After all, as the inheritance adage goes, “All toasters are appliances but not all appliances are toasters”…

What do I need to do to get a list of ShelfAwarenessReviews to exit the method in as a list of its parent type (Review)?

Just an FYI, the code calling this method is intended not to care about the types reviews it is getting. The subsequent code will operate on whatever.

I appreciate it big time.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T00:00:16+00:00Added an answer on May 27, 2026 at 12:00 am

    Well to start with, you don’t need to create an empty List<Review> which you then ignore 🙂

    Here’s the simplest solution:

    public List<Review> GetReviews(string filePath)
    {
        XElement xmlDoc = XElement.Load(filePath);
    
        var dtos = ...; // As before
    
        return dtos.Cast<Review>().ToList();
    }
    

    If you’re using .NET 4 and C# 4, there’s another alternative due to generic covariance which works for IEnumerable<T> but not List<T>:

    public List<Review> GetReviews(string filePath)
    {
        XElement xmlDoc = XElement.Load(filePath);
    
        IEnumerable<Review> dtos = ...; // As before
    
        return dtos.ToList();
    }
    

    Note the explicit specification of the type of dtos. The query expression will be of type IEnumerable<ShelfAwarenessReview> but that’s implicitly convertible (in C# 4) to IEnumerable<Review>.

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

Sidebar

Related Questions

I have the following php code, which has been snipped for brevity. I am
I have an mxml component PresentationWindow that is for brevity's sake, simply: <fx:Script> //...
Note: For brevity's sake, the following will not discern between randomness and pseudo-randomness. Also,
First high-level: I have a web page that will be showing correspondance between a
I'm creating a class that will manage a connection to a Firebird database. The
I have a WCF service which exposes a Generic interface (and the service has
I have a vector-like class that contains an array of objects of type T
A designer has given me a Silverlight child window that was designed in Expression
One of the annoying things about php's DateTime class is that it doesn't have
We have a web app built on Jboss that has been in production for

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.