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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:57:59+00:00 2026-05-25T19:57:59+00:00

I need to compare two DateTime properties in a linq query, similar to the

  • 0

I need to compare two DateTime properties in a linq query, similar to
the one below –

var patients = from c in session.Query<Patient>() where c.DateAdded.AddDays(1) < c.AdmitDate select c;

when I run the query I get this exception:
System.NotSupportedException {“System.DateTime AddDays(Double)”}

at
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression
expression)

I took a look at Fabio’s article on
http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-exten…
but the treeBuilder doesn’t have any functions that are specific to
DateTime comparisons.

Here is the code for the sample. To run this , install NuGet packages for FluentNhibernate and SQLite.

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace ConsoleApplication1
{
    class Program
    {
            private static Configuration _config;

            static void Main(string[] args)
            {
                    var sessionFactory = CreateSessionFactory();
                    using (var session = sessionFactory.OpenSession())
                    {
                            BuildSchema(session);
                            using(var transaction = session.BeginTransaction())
                            {
                                    var foo = new Patient
                                    {
                                            Name = "Foo",
                                            Sex = Gender.Male,
                                            DateAdded = new DateTime(2009, 1, 1),
                                            AdmitDate = new DateTime(2009, 1, 2)
                                    };
                                    var bar = new Patient
                                    {
                                            Name = "Bar",
                                            Sex = Gender.Female,
                                            DateAdded = new DateTime(2009, 1, 1),
                                            AdmitDate = new DateTime(2009, 1, 2)
                                    };
                                    session.SaveOrUpdate(foo);
                                    session.SaveOrUpdate(bar);
                                    transaction.Commit();
                            }
                            session.Flush();

                            using (session.BeginTransaction())
                            {
                                    var cats = from c in session.Query<Patient>() where
c.DateAdded.AddDays(1) < c.AdmitDate select c;
                                    foreach (var cat in cats)
                                    {
                                            Console.WriteLine("patient name {0}, sex    {1}", cat.Name,
cat.Sex);
                                    }
                            }
                    }
                    Console.ReadKey();
            }

            private static ISessionFactory CreateSessionFactory()
            {
                    return Fluently.Configure()
                      .Database(
                            SQLiteConfiguration.Standard.InMemory()
                      )
                      .Mappings(m =>
                            m.FluentMappings.AddFromAssemblyOf<Program>())
                      .ExposeConfiguration(c => _config = c)
                      .BuildSessionFactory();
            }

            private static void BuildSchema(ISession session)
            {
                    new SchemaExport(_config)
                      .Execute(true, true, false, session.Connection, null);
            }
    }

    public class PatientMap : ClassMap<Patient>
    {
            public PatientMap()
            {
                    Id(x => x.Id);
                    Map(x => x.Name)
                      .Length(16)
                      .Not.Nullable();
                    Map(x => x.Sex);
                    Map(x => x.DateAdded);
                    Map(x => x.AdmitDate);
            }
    }

    public class Patient
    {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual Gender Sex { get; set; }
            public virtual DateTime DateAdded { get; set; }
            public virtual DateTime AdmitDate { get; set; }
    }

    public enum Gender
    {
            Male,
            Female
    } 

Thanks,
Vikram

  • 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-25T19:57:59+00:00Added an answer on May 25, 2026 at 7:57 pm

    Answer to the above question:

    using System;
    using System.Linq;
    using System.Reflection;
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;
    using FluentNHibernate.Mapping;
    using NHibernate;
    using NHibernate.Cfg;
    using NHibernate.Dialect;
    using NHibernate.Dialect.Function;
    using NHibernate.Hql.Ast;
    using NHibernate.Linq.Functions;
    using NHibernate.Tool.hbm2ddl;
    using NHibernate.Linq;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using NHibernate.Linq.Visitors;
    using NHibernate.Cfg.Loquacious;
    
    namespace ConsoleApplication1
    {
    class Program
    {
        private static Configuration _config;
    
        static void Main(string[] args)
        {App_Start.NHibernateProfilerBootstrapper.PreStart();
    
            var sessionFactory = CreateSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                BuildSchema(session);
                using(var transaction = session.BeginTransaction())
                {
                    var foo = new Patient
                    {
                        Name = "Foo", 
                        Sex = Gender.Male, 
                        DateAdded = new DateTime(2009, 1, 4), 
                        AdmitDate = new DateTime(2009, 1, 6)
                    };
                    var bar = new Patient
                    {
                        Name = "Bar", 
                        Sex = Gender.Female, 
                        DateAdded = new DateTime(2009, 1, 1), 
                        AdmitDate = new DateTime(2009, 1, 2)
                    };
                    session.SaveOrUpdate(foo);
                    session.SaveOrUpdate(bar);
                    transaction.Commit();
                }
                session.Flush();
    
                using (session.BeginTransaction())
                {
                    //x.PatientVisit.AdmitDate.Value.Date == x.DateAdded.Date
                    var patients = from c in session.Query<Patient>() where c.DateAdded.AddDays(1) < c.AdmitDate.Value select c; 
                    foreach (var cat in patients)
                    {
                        Console.WriteLine("patient name {0}, sex  {1}", cat.Name, cat.Sex);
                    }
                }
            }
            Console.ReadKey();
        }
    
        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.Dialect<CustomDialect>()
                        .ConnectionString("Data Source=.;Initial Catalog=testdb;Integrated Security=True;Connection Reset=false")
                )
                .Mappings(m =>
                          m.FluentMappings.AddFromAssemblyOf<Program>())
                .ExposeConfiguration(c =>
                                        {
                                            c.LinqToHqlGeneratorsRegistry<ExtendedLinqtoHqlGeneratorsRegistry>();
                                            _config = c;
                                        })
                .BuildSessionFactory();
        }
    
        private static void BuildSchema(ISession session)
        {
            new SchemaExport(_config)
              .Execute(true, true, false, session.Connection, null);
        }
    }
    
    public class PatientMap : ClassMap<Patient>
    {
        public PatientMap()
        {
            Id(x => x.Id);
            Map(x => x.Name)
              .Length(16)
              .Not.Nullable();
            Map(x => x.Sex);
            Map(x => x.DateAdded);
            Map(x => x.AdmitDate);
        }
    }
    
    public class Patient
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Gender Sex { get; set; }
        public virtual DateTimeOffset DateAdded { get; set; }
        public virtual DateTime? AdmitDate { get; set; }
    }
    
    public enum Gender
    {
        Male,
        Female
    }
    
    public class ExtendedLinqtoHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
    {
        public ExtendedLinqtoHqlGeneratorsRegistry()
        {
            this.Merge(new AddDaysGenerator());
        }
    }
    
    public class AddDaysGenerator : BaseHqlGeneratorForMethod
    {
        public AddDaysGenerator()
        {
            SupportedMethods = new[] {
                ReflectionHelper.GetMethodDefinition<DateTimeOffset?>(d => d.Value.AddDays((double)0))
            };
        }
    
        public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
        {
            return treeBuilder.MethodCall("AddDays",
                                          visitor.Visit(targetObject).AsExpression(),
                                          visitor.Visit(arguments[0]).AsExpression()
                                          );
        }
    }
    
    public class CustomDialect : MsSql2008Dialect
    {
        public CustomDialect()
        {
            RegisterFunction(
                "AddDays",
                new SQLFunctionTemplate(
                    NHibernateUtil.DateTime,
                    "dateadd(day,?2,?1)"
                    )
                );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need compare two date. One is from databese, second is from now. var
I need to compare two bitmaps. One bitmap is loaded from a file, the
in bash I need to compare two float numbers, one which I define in
i need to compare two objects but compare a number of their properties in
I need to compare two account number columns from two different tables to see
I need to compare two datetime values to determine equality(exactly the same),using minute precision.Would
I need to compare two datetime variable only for some criteria : Day of
I need to compare two version #'s to see if one is greater than
I need to compare two files based on datetime. I need to check whether
I need to compare two files based on datetime upto minute by eliminating seconds

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.