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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:32:55+00:00 2026-05-20T01:32:55+00:00

This is a question related to another one I asked that was specific to

  • 0

This is a question related to another one I asked that was specific to NHibernate, but I’m starting to think that my question might be far more fundamental than that.

ForNHibernate-related reasons, I have a base abstract class (Listing) and classes that inherit from it (Listing_UK, Listing_US, etc).
These are functionally identical.
I understand why this does not work:

List<Listing> test = new List<Listing_UK>();

Right now I’m doing the equivalent of this:

List<Listing> test = new List<Listing>();
test.Add(new Listing_UK() as Listing);

which works, but I need the classes to be more interchangeable, like the above.

I appreciate that the fundamental idea behind what I’m doing is a little weird, but does anyone have any advice?

EDIT:

I’ve clearly made my examples way too abstract.
I was trying to avoid making this too NHibernate-specific so that it’s actually different from my other question (and because I think what I’m asking is more fundamental), but what I basically want to achieve is this:

IQueryOver<Listing,Listing> test = null;
if(condition) {
  test = DBSession.QueryOver<Listing_UK>();
} else {
  test = DBSession.QueryOver<Listing_US>();
}
test.Where(l => l.Field == "value").List<Listing>();

From the reactions I am getting here, I’m reasonably sure that what I’m asking for is not possible.

  • 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-20T01:32:55+00:00Added an answer on May 20, 2026 at 1:32 am

    There’s no need to use “as” in the code you’ve got. This will work fine:

    List<Listing> test = new List<Listing>();
    test.Add(new Listing_UK());
    

    It’s not really clear what you need to do which you can’t do already. Do you actually need a List<Listing_UK>? What’s the bigger picture here? If you could show some code which doesn’t work, that would help.

    EDIT: Okay, now that you’ve explained a bit more, I suspect you want a generic method:

    List<Listing> list = condition ? QueryList<Listing_UK>() 
                                   : QueryList<Listing_US>();
    
    private static List<Listing> QueryList<T>() where T : Listing
    {
        return DBSession.QueryOver<T>()
                        .Where(l => l.Field == "value")
                        .List<Listing>();
    }
    

    I don’t know NHibernate well enough to know whether that’s enough already, but if not I’m sure you could adjust it slightly.

    Your original thought of just trying to treat the types interchangably really isn’t going to work.

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

Sidebar

Related Questions

This is an offshoot question that's related to another I asked here . I'm
This question is related to another question which I asked yesterday! List all links
[This question is related to but not the same as this one .] My
This question is related to this one , though I think I was a
[This question is related to but not the same as this one .] If
This is another question related to a question I asked a few minutes ago.
I've asked another related question to this here: casting dynamic to static problem I've
This question is related to (but perhaps not quite the same as): Does Django
This question is related to this other question I recently asked... cf.net exception and
I asked a related question before, but I guess the root of the question

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.