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)
   at
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression
expression)
   at
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression
expression)
   at
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression
expression)
   at
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(Expression
expression, VisitorParameters parameters)
   at
NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause
whereClause, QueryModel queryModel, Int32 index)
   at Remotion.Data.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor
visitor, QueryModel queryModel, Int32 index)
   at
Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1
bodyClauses, QueryModel queryModel)
   at
Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel
queryModel)
   at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
   at
NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel
queryModel, VisitorParameters parameters, Boolean root)
   at
NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor
sessionFactory)
   at
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String
queryIdentifier, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 filters,
ISessionFactoryImplementor factory)
   at
NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String
expressionStr, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 enabledFilters,
ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
expressionStr, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 enabledFilters,
ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
expressionStr, IQueryExpression queryExpression, Boolean shallow,
IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at
NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at
NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression
queryExpression)
   at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression
expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression
expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at ConsoleApplication1.Program.Main(String[] args) in c:\Users
\vnayak\Documents\Visual Studio 2010\Projects
\ConsoleApplication1\ConsoleApplication1\Program.cs:line 52
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
   at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I took a look at Fabio's article on
http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html;
but the treeBuilder doesnot have any functions that are specific to
DateTime comparisons.

Thanks,
Vikram


To run this , import the FluentNhibernate and SQLite Nuget pagckages.

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
        }
}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to