I’m using Fluent NHibernate (1.2.0.712) and Nhibernate.Linq (version 1) with SQLite (1.0.79)
Here is my model:
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; }
}
Note that the ID is a long.
I also have this bit of 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 is a data layer repository implementing IQueryable.
This query works fine. However, when I looked at the generated SQL, I saw this:
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)]
The question is; why is the ID getting cast to an INTEGER?
In fact, why is it casting at all?
On the code side, the g.Min(i => i.ID) knows that it is returning a long.
A new anonymous type is being generated to hold the result and if I do a .elementAt(0).ID on it then it gives me a long as well so that all seems fine.
You are seeing the conversion because long is not a sql data type. I understand that your SQLite columns can be typeless with exception to the ID but NHibernate converts .NET data types to their sql equivalent. I would suggest using Int64 instead of long just to be safe but this is expected behavior.