流利的nhibernate:添加日期 [英] Fluent nhibernate: adding date

查看:246
本文介绍了流利的nhibernate:添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Fluent NHibernate,我想查看一个给定日期的未来日期。为简单起见,我已将代码减少到最低限度。属性MyDate映射到日期类型的Sql Server中的列。但是,我使用Sql TypeDateTime尝试了下面相同的代码,但我仍然得到了相同的结果。我正在尝试使用以下代码:



  var  myTbl = IQuerable< MYTABLE>(); 
var result = myTbl.Where(x = > SqlFunctions.DateAdd( month 6 ,x.MyDate)。值< = DateTime.Now);





错误不是但是,在调试时,当我尝试查看变量结果的Result View时,我可以在监视窗口中看到以下内容{PartialEvalException(NotSupportedException(\)此函数只能从LINQ to Entities调用。\),DateAdd(\dd \,6,31 / 10/20 20:33:05))} System.SystemException {System.NotSupportedException}



任何帮助都将不胜感激!



我尝试过:



我尝试使用以下代码对上述内容进行了修改,但是,所有代码都导致NHibernate中出现错误。



  var  result1 = myTable.Where(x = >  SqlFunctions .DateAdd(  month 6  ,x.MyDate)<  = DateTime.Now); 

var result2 = myTable.Where(x = > SqlFunctions.DateAdd ( month 6 ,DateTime 。现在< = DateTime.Now);

var result3 = myTable.Where(x = > SqlFunctions.DateAdd ( month 6 ,x .MyDate)!= null && SqlFunctions.DateAdd( 6 ,x.MyDate)< = DateTime.Now );

var result4 = myTable.Where(x = > EntityFunctions.AddMonths (x.myDate, 6 < = DateTime.Now);

解决方案

SqlFunctions 仅适用于LINQ to SQL; EntityFunctions 只能从Entity Framework调用。



如果你想从NHibernate调用一个特定的函数,然后你需要创建一个自定义方言:

Pure Dot Net Coder:在NHibernate Linq上使用DateAdd [ ^ ]



或者,更改您的标准:

< pre lang =C#> var maxDate = DateTime.Now.AddMonths(-6);
var result1 = myTable.Where(x = > x.MyDate < = maxDate);



这样做的好处是可以在日期列上使用索引或分区满足查询,而不必阅读表中的每条记录。


Using Fluent NHibernate, I would like to check a future date with a give date. For simplicity's sake, I've reduced my code to the bare minimum below. The property "MyDate" is mapped to a column in Sql Server of type "Date". However, I've tried the same code below using the Sql Type "DateTime" but I still got the same result. I'm trying using the below code:

var myTbl = IQuerable<mytable>();
var result = myTbl.Where(x => SqlFunctions.DateAdd("month", 6, x.MyDate).Value <= DateTime.Now);



An error is not being throw, however, while debugging, when I try to see the Result View of the variable result, I can see the following in the watch window "{"PartialEvalException (NotSupportedException (\"This function can only be invoked from LINQ to Entities.\"), DateAdd(\"dd\", 6, 31/10/2016 20:33:05))"} System.SystemException {System.NotSupportedException}"

Any help would be greatly appreciated!

What I have tried:

I tried variations to the above with the following code, however, all code resulted in errors being thrown in NHibernate.

var result1 = myTable.Where(x => SqlFunctions.DateAdd("month", 6, x.MyDate) <= DateTime.Now);

var result2 = myTable.Where(x => SqlFunctions.DateAdd("month", 6, DateTime.Now) <= DateTime.Now);

var result3 = myTable.Where(x => SqlFunctions.DateAdd("month", 6, x.MyDate) != null && SqlFunctions.DateAdd("month", 6, x.MyDate) <= DateTime.Now);

var result4 = myTable.Where(x => EntityFunctions.AddMonths(x.myDate, 6) <= DateTime.Now);

解决方案

The SqlFunctions will only work with LINQ to SQL; the EntityFunctions can only be invoked from Entity Framework.

If you want to call a specific function from NHibernate, then you'll need to create a custom dialect:
Pure Dot Net Coder: Using DateAdd with NHibernate Linq[^]

Alternatively, change your criteria:

var maxDate = DateTime.Now.AddMonths(-6);
var result1 = myTable.Where(x => x.MyDate <= maxDate);


That will also have the benefit of being able to use an index or partition on the date column to satisfy the query, rather than having to read every record in the table.


这篇关于流利的nhibernate:添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆