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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:09:08+00:00 2026-06-18T02:09:08+00:00

I want to query with NHibernate Linq by component. The component contains a Date

  • 0

I want to query with NHibernate Linq by component. The component contains a Date property which is not included in the override Equals. I just want to skip the Date property when asking for equality. At the moment Date property is set in the ctor of the Address class and the query will return 0 rows because of that. I know that this breaks the Value Object concept but I just want to know why the code below does not work properly.

THE QUERY As you can see the Date property is in the query

select user0_.Id     as Id0_,
       user0_.Name   as Name0_,
       user0_.Number as Number0_,
       user0_.Date   as Date0_
from   test1 user0_
where  (user0_.Number = 1 /* @p0 */
        and user0_.Date = '2013-01-28T14:29:47.00' /* @p1 */)

MAIN

class Program
    {
        private static ISessionFactory _sessionFactory;

        private static void CreateSessionFactory()
        {
            FluentConfiguration config = Fluently
                .Configure(new Configuration().Configure())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>());
            new SchemaExport(config.BuildConfiguration()).Create(false, true);
            _sessionFactory = config.BuildSessionFactory();
        }

        [STAThread]
        static void Main(string[] args)
        {
            CreateSessionFactory();

            using (var session = _sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var user = new User()
                {
                    Name = "Nik",
                    Address = new Address(1)
                };
                session.Save(user);
                tx.Commit();
            }

            using (var session = _sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                session.Query<User>().Where(x => x.Address == new Address(1)).Single();
                tx.Commit();
            }
        }
    }

CLASSES AND MAPPINGS

public class Address : IEquatable<Address>, IEqualityComparer<Address>
    {
        protected Address() { }

        public Address(int number)
        {
            Number = number;
            Date = DateTime.Now;
        }

        public virtual int Number { get; protected set; }
        public virtual DateTime Date { get; protected set; }

        public override bool Equals(object obj)
        {
            return Number == ((Address)obj).Number;
        }

        public override int GetHashCode()
        {
            return Number.GetHashCode();
        }

        public bool Equals(Address other)
        {
            return Number == other.Number;
        }

        public bool Equals(Address x, Address y)
        {
            return x.Number == y.Number;
        }

        public int GetHashCode(Address obj)
        {
            return obj.GetHashCode();
        }
    }

    public class User
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Address Address { get; set; }
    }

    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("test1");
            Id(x => x.Id).GeneratedBy.GuidNative();
            Map(x => x.Name);
            Component(x => x.Address, cm =>
            {
                cm.Map(x => x.Number);
                cm.Map(x => x.Date);
            });
        }
    }
  • 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-18T02:09:09+00:00Added an answer on June 18, 2026 at 2:09 am

    You have mapped Address as a component. Therefore, when you ask NHibernate to compare user.Address to some Address instance, it will compare every attribute. NHibernate doesn’t analyse compiled code to determine which of the properties are used in the Equals() method.

    The code works properly.

    You can write the query to compare every attribute separately instead of comparing the entire addresses. Or try to rethink the design to make Address a true value object.

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

Sidebar

Related Questions

I want to translate following simple sql query into Linq to NHibernate: SELECT NewsId
I want to translate following query from nhibernate criteria query api to linq. var
I'm using a custom named query with NHibernate which I want to return a
i want query a file xml with linq, i want get all descendat of
I want to query the server to see if the date is different than
I want a query which will return a combination of characters and number Example:
I know you can execute a Linq to NHibernate query as a future by
How would you write this exact SQL query with the new Linq-to-NHibernate Provider (3.x)
I'm using NHibernate 3.0 with both the LINQ provider and QueryOver. Sometimes I want
If you do an nHiberante Linq query and you want to eager load the

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.