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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:50:49+00:00 2026-05-16T00:50:49+00:00

Possible Duplicate: Why do I get error: … must be a reference type in

  • 0

Possible Duplicate:
Why do I get “error: … must be a reference type” in my C# generic method?

I have 2 Repository methods that are almost identical:

public IList<Fund> GetFundsByName(int pageSize, string searchExpression)
{
    return _session.CreateCriteria<Fund>()
        .AddNameSearchCriteria<Fund>(searchExpression)
        .AddOrder<Fund>(f => f.Name, Order.Asc)
        .SetMaxResults(pageSize).List<Fund>();
}

public IList<Company> GetCompaniesByName(int pageSize, string searchExpression)
{
    return _session.CreateCriteria<Company>()
        .AddNameSearchCriteria<Company>(searchExpression)
        .AddOrder<Company>(f => f.Name, Order.Asc)
        .SetMaxResults(pageSize).List<Company>();
}

The only difference is that the first one’s _session.CreateCriteria is of type Fund and the second one is company

I was hoping that I could make this generic by changing the method definition to:

public IList<T> GetEntitiesByName<T>(int pageSize, string searchExpression)
    where T : ISearchableEntity
{
    return _session.CreateCriteria<T>()
        .AddNameSearchCriteria<T>(searchExpression)
        .AddOrder<T>(f => f.Name, Order.Asc)
        .SetMaxResults(pageSize).List<T>();
}

where ISearchableEntity is defined as:

public interface ISearchableEntity
{
    string Name { get; set; }
}

but unfortunately NHibernate doesn’t like this and gives me the error:

The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'NHibernate.ISession.CreateCriteria<T>()'

Is it possible for me to make this generic some other way?

  • 1 1 Answer
  • 1 View
  • 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-16T00:50:50+00:00Added an answer on May 16, 2026 at 12:50 am

    You could try adding the constraint class:

    where T : class, ISearchableEntity
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do I get must override a superclass method with @Override? I
Possible Duplicate: PHP ToString() equivalent I get this error: Catchable fatal error: Object of
Possible Duplicate: Error : get property of non-object I am new to codeigniter model,
Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: How to get the address of an overloaded member function? I have
Possible Duplicate: email zend framwork smtp I have following configuration: smtp.type = Zend_Mail_Transport_Smtp smtp.smtpServer
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have
Possible Duplicate: How to get useful error messages in PHP? I'm programming php on
Possible Duplicate: Get xpath location of element in iframe(iframe from my domain) I have
Possible Duplicate: Sort a Custom Class List<> I have a Generic Collector List and

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.