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

  • Home
  • SEARCH
  • 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 6642043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:00+00:00 2026-05-25T23:53:00+00:00

I have a base class, called NodeUpgrade , which have several child types. An

  • 0

I have a base class, called NodeUpgrade, which have several child types. An example of a specific child class is FactoryUpgrade.

I have a list of NodeUpgrades, which can be a mix of different child types. How do I write a linq query to retrieve a type of NodeUpgrade and cast to that specific type?

My working query looks something like this:

var allFactories = (from Node n in assets.Nodes
                    from FactoryUpgrade u in n.NodeUpgrades
                    where u.ClassID == NodeUpgradeTypes.Factory
                    select u)

This, of course, doesn’t work. Can I specify the final type of the output?

  • 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-25T23:53:01+00:00Added an answer on May 25, 2026 at 11:53 pm

    If you are sure that every type in a sequence is a given type, you can use the Cast<T>() extension method. If there can be multiple types in the list and you only want one of them, you can use OfType<T>() to filter the sequence.

    List<Animal> animals = ...
    
    // assumes all animals are cats 
    var cats = animals.Cast<Cat>();
    // var cats = (from animal in animals where ... select animal).Cast<Cat>();
    
    // or maybe animals can contain dogs, but you don't want them
    var cats = animals.OfType<Cat>();
    

    The difference is that Cast will throw an exception if an animal isn’t a cat, whereas OfType will perform a type check before actually trying the conversion. I would favor Cast over OfType when you are confident of the uniform type. (Also note that these do not perform user-defined conversions. If you have defined an implicit or explicit conversion, those will not be supported by these methods.)

    The resulting sequence in each case will be IEnumerable<Cat>, which you can do further query operations on (filters, groupings, projections, ToList(), etc.)

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

Sidebar

Related Questions

I have an abstract base class called Shape from which both Circle and Rectangle
I have a base class with a property called Name, which has an XmlText
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.
I have a base class called Component which has a number of classes derived
I have a base class called Geometry from which there exists a subclass Sphere
Basically I have a Base class called Program. I then have more specific program
I have a base class called LabFileBase. I have constructed a List and have
I have a base class called Component. I also have 2 interfaces I2DComponent, and
Ok so I have an abstract base class called Product, a KitItem class that
I have a class Contact (base class),a class called Customer and a class called

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.