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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:10:42+00:00 2026-06-17T01:10:42+00:00

I have been trying to create a calculated property in my persistence layer by

  • 0

I have been trying to create a calculated property in my persistence layer by follow
Hendry Luk’s solution for calculated properties.

I am able to select the values from the DB using a linq query:

var result  = from parcel in Repository.Query();

When I try to do a where on the selected result, I get a could not resolve property error.

Here is what my code looks like.

My Model:

 namespace ConsoleApplication14
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Linq.Expressions;
        using System.Text;

        public class Common1 : ICommon
        {
            public virtual int Id { get; set; }

            public virtual string Name { get; set; }

            //public static readonly Expression<Func<Common1, string>> CalculatedDisplayExpression = x => ("Common 1 Display: " + x.Id + " - " + x.Name);
            public static readonly Expression<Func<Common1, string>> CalculatedDisplayExpression = x => (x.Id + "");

            private static readonly Func<Common1, string> CalculateDisplay = CalculatedDisplayExpression.Compile();

            public virtual string Display { get { return CalculateDisplay(this); } }
        }
    }

My Interface the model implementing:

namespace ConsoleApplication14
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;`enter code here`

    public interface ICommon
    {
        int Id { get; set; }
        string Name { get; set; }
        string Display { get;  }
    }
}

Model’s mapping

namespace ConsoleApplication14
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate.Mapping.ByCode;
    using NHibernate.Mapping.ByCode.Conformist;

    public class Common1Map : ClassMapping<Common1>
    {
        public Common1Map()
        {
            Id(x => x.Id, map => map.Generator(Generators.Native));
            Property(x => x.Name);
        }
    }
}



namespace ConsoleApplication14
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Text;
    using NHibernate.Hql.Ast;
    using NHibernate.Linq;
    using NHibernate.Linq.Functions;
    using NHibernate.Linq.Visitors;

    public class CalculatedPropertyGenerator<T, TResult> : BaseHqlGeneratorForProperty
    {
        public static void Register(ILinqToHqlGeneratorsRegistry registry, Expression<Func<T, TResult>> property, Expression<Func<T, TResult>> calculationExp)
        {
            registry.RegisterGenerator(ReflectionHelper.GetProperty(property), new CalculatedPropertyGenerator<T, TResult> { _calculationExp = calculationExp });
        }

        private CalculatedPropertyGenerator() { }

        private Expression<Func<T, TResult>> _calculationExp;

        public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
        {
            return visitor.Visit(_calculationExp);
        }
    }
}


namespace ConsoleApplication14
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate;
    using NHibernate.Cfg;
    using NHibernate.Cfg.MappingSchema;
    using NHibernate.Dialect;
    using NHibernate.Driver;
    using NHibernate.Tool.hbm2ddl;
    using NHibernate.Mapping.ByCode;
    using NHibernate.Mapping;
    using Iesi.Collections.Generic;
    using System.Reflection;
    using NHibernate.Linq.Functions;
    using NHibernate.Linq;

    public class SessionProvider
    {
        private static ISessionFactory sessionFactory;

        public static SessionProvider Instance { get; private set; }
        //DefaultLinqToHqlGeneratorsRegistry registry = new DefaultLinqToHqlGeneratorsRegistry();

        ILinqToHqlGeneratorsRegistry registry = new DefaultLinqToHqlGeneratorsRegistry();

        static SessionProvider()
        {
            var provider = new SessionProvider();
            provider.Initialize();
            Instance = provider;
        }

        private SessionProvider() { }

        private void Initialize()
        {
            const string connString = "server=(local)\\mssql2008;database=Common;integrated security=sspi";

            Configuration configuration = new Configuration();
            configuration
                .DataBaseIntegration(db =>
                {
                    db.ConnectionString = connString;
                    db.Dialect<MsSql2008Dialect>();
                    db.Driver<SqlClientDriver>();
                    db.LogSqlInConsole = true;
                    db.IsolationLevel = System.Data.IsolationLevel.ReadCommitted;
                })
                .AddDeserializedMapping(GetMappings(), null);


            CalculatedPropertyGenerator<Common1, string>.Register(registry, x => x.Display, Common1.CalculatedDisplayExpression);

//            registry.RegisterGenerator(ReflectionHelper.GetProperty<Common1, string>(x => x.Display), new CalculatedPropertyGenerator());




            var exporter = new SchemaExport(configuration);
            exporter.Execute(true, true, false); 
            sessionFactory = configuration.BuildSessionFactory();
        }

        private HbmMapping GetMappings()
        {
            ModelMapper mapper = new ModelMapper();
            mapper.AddMappings(Assembly.GetAssembly(typeof(Common1Map)).GetExportedTypes());
            HbmMapping mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();

            return mappings;
        }

        public ISession OpenSession()
        {
            return sessionFactory.OpenSession();
        }
    }
}

Client:

namespace ConsoleApplication14
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate;
    using NHibernate.Linq;
    using NHibernate.Linq.Functions;

    public class Tester
    {
        private ILinqToHqlGeneratorsRegistry registry = new DefaultLinqToHqlGeneratorsRegistry();
        public void Go()
        {
            using (ISession session = SessionProvider.Instance.OpenSession())
            {
                CreateData(session);

                IQueryable<ICommon> commons = session.Query<ICommon>();//.Where(x => x.Display.Contains("Common1 #7"));

                var common1 = session.Query<Common1>().Where(x => x.Display.Contains("Common1 #7"));

                foreach(var common in commons)
                {
                    Console.WriteLine(common.Display);
                }
            }
        }

        private void CreateData(ISession session)
        {
            using (ITransaction tx = session.BeginTransaction())
            {
                for (int i = 0; i < 10; i++)
                {
                    session.SaveOrUpdate(new Common1() { Name = "Common1 #" + i });
                }

                tx.Commit();
            }
        }
    }
}
  • 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-17T01:10:43+00:00Added an answer on June 17, 2026 at 1:10 am

    Register in SessionProvider class, like

    configuration.LinqToHqlGeneratorsRegistry<MyLinqToHqlGeneratorsRegistry>(); 
    

    MyLinqToHQLGeneratorRegistry implementation looks like this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate.Linq.Functions;
    using NHibernate.Linq;
    
    namespace ConsoleApplication14
    {
        public class MyLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
        {
            public MyLinqToHqlGeneratorsRegistry()
                : base()
            {
    
                CalculatedPropertyGenerator<Common1, string>.Register(this, x => x.Display, Common1.CalculatedDisplayExpression);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a ListView which I can sort using drag
I have been trying to create a menu panel with jQuery that can be
I have been trying to create a new google custom search engine, but when
I have been trying to create a simple program with Python which uses OpenCV
I have been trying to create an observable tweeter feed using tweetsharp with the
I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php
Hi I have been trying to create a crossfade plugin with previous and next
I have been trying for almost a week now to create an SQLite database
I have been trying to find a good guide to create a templated control.
Ok I'm stumped. I have been trying to use Simpletip to create a tooltip

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.