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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:48:58+00:00 2026-05-27T17:48:58+00:00

I have an object called Market in my C# 4 application: public class Market

  • 0

I have an object called Market in my C# 4 application:

public class Market : BusinessBase
{
    public Market()
    {
    }

    public virtual int Id { get; set; }
    public virtual string Symbol { get; set; }
    public virtual string Description { get; set; }
}

I have a table called markets in SQL Server:

CREATE TABLE [dbo].[markets](
    [marketId] [int] IDENTITY(1,1) NOT NULL,
    [symbol] [varchar](10) NOT NULL,
    [description] [varchar](30) NOT NULL,
    [version] [int] NOT NULL,
 CONSTRAINT [PK_markets] PRIMARY KEY CLUSTERED 
    ([marketId] ASC)
GO
CREATE UNIQUE NONCLUSTERED INDEX [NIDX_markets_symbol] ON [dbo].[markets] ([symbol] ASC)
GO

My mapping file looks like this:

<class name="MooDB.BusinessLayer.Market" table="markets">
    <id name="Id" column="marketId" type="Int32">
        <generator class="native" />
    </id>           
    <version name="Version" column="version" type="integer" unsaved-value="0" />
    <property name="Symbol" column="symbol" type="string" length="10" />
    <property name="Description" column="description" type="string" length="30" not-null="true" />        
</class>

I can retrieve a market from the database by Id by simply using this method:

public Market GetMarketById(int marketId)
{
    ISession session = GetSession();
    return session.Get<Market>(marketId);
}

I want to do the same thing but pass in the Symbol instead. Symbol is enforced unique in the database and I want to return a Market object. I am trying this code:

public Market GetMarketBySymbol(string symbol)
{
    ISession session = GetSession();
    return session.Get<Market>(symbol);
}

I know this is wrong but is there a way to return one object which maps to one unique row in the database but not retrieve it by PK? I could simply get an <IList>of Market objects and then just return the one using a ‘foreach’ but that feels inelegant to me.

  • 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-27T17:48:59+00:00Added an answer on May 27, 2026 at 5:48 pm

    If you are using NH 3+ you can use LINQ:

    return session.Query<Market>().SingleOrDefault(w => w.Symbol == symbol);
    

    Or QueryOver:

    return session.QueryOver<Market>()
      .Where(w => w.Symbol == symbol).SingleOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object called Contact. A Contact has a relationship with a set
I have an object class called BOM. It has a method, getChildItem() which returns
I have the following situation: public abstract class A { private Object superMember; public
I have a CognitiveEntity class, defined this way: class CognitiveEntity : public Object {
I have an object called Layer that has some attributes and some methodes. i
I have an object called Document that has a one-to-many relationship with DocumentPersonCc. I
I have a object called: SqlCeDataReader rdr; Because SqlCeDataReader does not have HasRows function,
I have an object called users that I use to store user objects that
Here is the scenario: I have an object called a Transaction that needs to
I'm having an issue with an if statement I have an object called enemy2

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.