0

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.

Steve
  • 1,266
  • 16
  • 37

1 Answers1

1

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.

Justin
  • 2,093
  • 1
  • 16
  • 13
  • You're absolutely right, I checked the table definitions and they are all typed as INTEGER rather than BIGINT (perhaps that's an MSSQL only type? I'm from an MSSQL background so just assumed it would use that.) – Steve Apr 20 '12 at 12:32
  • 1
    I've had a little look around at similar topics and found this: http://stackoverflow.com/questions/7337882/sqlite-and-integer-types-int-integer-bigint that explains more about how SQLite handles types. – Steve Apr 20 '12 at 12:35
  • Yeah, I'm from a MSSQL background too, glad I could help. – Justin Apr 20 '12 at 13:44