我需要在LINQ查詢兩個日期時間屬性,類似於比較 下一個 -NHibernate的LINQ DateTime.AddDay不起作用
var patients = from c in session.Query<Patient>() where c.DateAdded.AddDays(1) < c.AdmitDate select c;
當我運行查詢我得到這個異常: 系統。 NotSupportedException異常{ 「的System.DateTime AddDays(雙)」}
在 NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression 表達)
我看了一下Fabio的文章 http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-exten ... 但是treeBuilder沒有任何特定於 DateTime比較的函數。
以下是樣本代碼。要運行這個,爲FluentNhibernate和SQLite安裝NuGet包。
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
}
感謝, 維克拉姆
看起來它已經在這裏報道:https://nhibernate.jira.com/瀏覽/ NH-2849您是否嘗試過使用'QueryOver'代替? – mxmissile