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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:53:18+00:00 2026-05-16T17:53:18+00:00

The Example class is great if you are specifying which properties you want to

  • 0

The Example class is great if you are specifying which properties you want to exclude from the example. But what if you want to specify which properties to include?

Take this example: looking for people in the database that have the same name.
A Person object has many properties. So to use the NHibernate.Criterion.Example object I would have to specify every field to exclude – which could be many.

Why is there no IncludeProperty method?

I have a Person object and I want to see if it is a duplicte based on pre-set business rules (FirstName, LastName, DateOfBirth). These rules could be changed to include a postcode or something else – and I’d like to make that configurable.

Is there an easy way around 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-16T17:53:19+00:00Added an answer on May 16, 2026 at 5:53 pm

    I have a solution to the IncludeProperty issue:

    private Type persitentType = typeof(T);
    public IList<T> GetByExample(T exampleInstance, params string[] propertiesToInclude)
    {
        // get the properties that will be excluded
        List<string> propertiesToExclude =
            persitentType.GetProperties().Where(p => propertiesToInclude.Contains(p.Name) == false).Select(p => p.Name).ToList();
    
        // create the criteria based on the example and excluding the given properties
        ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
        Example example = Example.Create(exampleInstance);
        foreach (string propertyToExclude in propertiesToExclude)
        {
            example.ExcludeProperty(propertyToExclude);
        }
        criteria.Add(example);
    
        // return the result 
        return criteria.List<T>();
    }
    

    Add this method to your repository class. It uses reflection to determine what properties the specified object has, and then finds the properties to exclude based on those that have been specified as includes.

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

Sidebar

Related Questions

Let's have a following example: public class X { } public class Y {
in C# 3, initializers were added. This is a great feature. However, one thing
I've been trying to follow the principles of Dependency Injection, but after reading this
What is a prototype for a JavaScript class? In other words, what is the
I was reading this blog post: http://www.shakie.co.uk/ramblings/feature-driven-development-riding-the-waves-of-change/ and came across the part about TestDox:
I have an object from my domain model that has a child object. How
I went to all the documentation, also I went to the IRC channel (BTW
I came across some code recently that used a custom error handler to turn
I have a design question concerning Django. I am not quite sure how to
I have a list of objects. These objects have three variables, ID, Name, &

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.