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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:23:09+00:00 2026-06-11T21:23:09+00:00

Please excuse my ignorance when asking this question as I am still learning NHibernate

  • 0

Please excuse my ignorance when asking this question as I am still learning NHibernate and Linq. I did some searching around but I do not understand how or if the solution to my question is possible

I have the following block of code:

// this function searches the database's table for a single object that matches the 'Name' property with 'objectName'
public static Object Read<T>(string objectName)
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        IQueryable<T> objectList = session.Query<T>(); // pull (query) all the objects from the table in the database
        int count = objectList.Count(); // return the number of objects in the table
        // alternative: int count = makeList.Count<T>();

        IQueryable<T> objectQuery = null; // create a reference for our queryable list of objects
        object foundObject = null; // create an object reference for our found object

        if (count > 0)
        {
            // give me all objects that have a name that matches 'objectName' and store them in 'objectQuery'
            objectQuery = (from obj in objectList where obj.Name == objectName select obj);

            // make sure that 'objectQuery' has only one object in it
            try
            {
                foundObject = objectQuery.Single();
            }
            catch
            {
                return null;
            }

            // output some information to the console (output screen)
            Console.WriteLine("Read Make: " + foundObject.ToString());
        }
        // pass the reference of the found object on to whoever asked for it
        return foundObject;
    }
}

This is all great with the exception of one line:
objectQuery = (from obj in objectList where obj.Name == objectName select obj);

The issue here is that I am asking for the “Name” property of an unknown object and that is not only impossible but also erroneous code.

What I’d really like to do here is specify that I am looking for items that have a property belonging to the T-type object.

Any takers?

  • 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-06-11T21:23:11+00:00Added an answer on June 11, 2026 at 9:23 pm

    The problem is that you are writing a generic method, which implies it will accept any type T. But in fact, when you write:

    where obj.Name == objectName
    

    you are expecting a particular type, or a type that implements a specific interface, or is derived from a particular base class.

    Your method should look more like:

    public static T Read<T>(string objectName)
    where T : ISomeInterface
    

    Note that I have also changed the return type from Object to T, which is more friendly to the caller of this method and avoids the need for them to cast Object to T.

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

Sidebar

Related Questions

Please excuse the newbie question but I was wondering if I used Linq in
Please excuse my ignorance, but I have come across this term owner drawn listbox.
I'm a Linq noob so please excuse my ignorance but I need to group
Please excuse my ignorance.. i know a fair bit but am somehow still hazy
First, please excuse my ignorance in this field, I'm a programmer by trade but
Please excuse the noob-ish nature of this question. I have a project in which
this was my first dojo build so please excuse my ignorance in this matter.
This is my first time dabbling in NLP so please excuse my ignorance. I'm
Please excuse the simplicity and length of this, but I have a little test
Excuse my ignorance but I'm having a tough time figuring this out. I'm trying

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.