【发布时间】:2012-03-28 08:32:18
【问题描述】:
我在 SQLite (1.0.79) 中使用 Fluent NHibernate (1.2.0.712) 和 Nhibernate.Linq(版本 1)
这是我的模型:
public class QueueItem
{
public virtual long ID { get; set; }
public virtual DateTime AddedToQueue { get; set; }
public virtual DateTime DontProcessUntil { get; set; }
public virtual DataQueueItemState State { get; set; }
}
请注意,ID 很长。 我也有这个 LINQ:
var nextID =
from i in _repository
where i.State == DataQueueItemState.GetDataQueue && i.DontProcessUntil < DateTime.UtcNow
group i by i.State into g
select new { ID = g.Min(i => i.ID) };
_repository 是实现 IQueryable 的数据层存储库。
此查询工作正常。然而,当我查看生成的 SQL 时,我看到了这个:
NHibernate: select cast(min(queueitem0_.ID) as INTEGER) as col_0_0_ from "QueueItem"
queueitem0_ where queueitem0_.State=@p0 and queueitem0_.DontProcessUntil<@p1 group by
queueitem0_.State;@p0 = 'GetDataQueue' [Type: String (0)], @p1 = 28/03/2012 08:21:10
[Type: DateTime (0)]
问题是;为什么将 ID 转换为 INTEGER? 事实上,它为什么要强制转换?
在代码方面,g.Min(i => i.ID) 知道它正在返回一个 long。 正在生成一个新的匿名类型来保存结果,如果我对其执行 .elementAt(0).ID ,那么它也会给我一个 long ,这样一切看起来都很好。
【问题讨论】: