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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:23:38+00:00 2026-05-13T08:23:38+00:00

I am try to convert the following NHibernate query using dyanmic instantiation into an

  • 0

I am try to convert the following NHibernate query using dyanmic instantiation into an IList<t> rather than an IList.

IList<AllName> allNames =
  (IList<AllName>)Session.CreateQuery(
  @"select new AllName(
  name.NameId, name.FirstName, origin.OriginId, origin.Description,
  name.Sex, name.Description, name.SoundEx
  ) from Name name join name.Origin origin")
  .SetFirstResult(skip)
  .SetMaxResults(pageSize);

Running this I get the following error:-

Unable to cast object of type
‘NHibernate.Impl.QueryImpl’ to type
‘System.Collections.Generic.IList`1[Domain.Model.Entities.AllName]’.

I know that I can return

IList results = Sesssion.CreateQuery(...

but my service layers expect an

IList<AllName>

How can I achieve this?

  • 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-13T08:23:39+00:00Added an answer on May 13, 2026 at 8:23 am

    One option would be to write an IList<T> implementation which proxies to an IList, casting where appropriate. It shouldn’t be too hard to do, albeit somewhat tedious. LINQ will make this slightly easier in terms of implementing IEnumerable<T> etc – you can just call the underlying iterator and call Cast<T>() on it, for example.

    Another solution would be to create a new List<T> from the returned list:

    IList query = Session.CreateQuery(...);
    IList<AllName> allNames = new List<AllName>(query.Cast<AllName>());
    

    EDIT: As Sander so rightly points out, this is what ToList is for:

    IList query = Session.CreateQuery(...);
    return query.Cast<AllName>().ToList();
    

    EDIT: All of this has been NHibernate-agnostic, as it were – I don’t actually know much about NHibernate. Rippo has now found a much simpler answer – call List<AllName> instead (which is an NHibernate method).

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

Sidebar

Related Questions

My problem is the following. I try to convert a string into double. In
I have the following code where I try to convert the string 03080000 into
I need to convert the following two working queries into a single query but
We try to convert from string to Byte[] using the following Java code: String
I'm using Lyx to produce a Latex document, and when i try to convert
When I try to convert a NSString object into a NSdata object, I use
I am getting a NullReferenceException when I try and convert the following LINQ to
I try to convert a string into a date in t-sql. However get results
I try to convert an svg into PNG. the svg document is coming from
how do i convert the following datetime string that contains a timezone designation into

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.