views:

185

answers:

1

I'm just starting out with Linq to NHibernate and having troubles with a pretty simple query. I have a database column that is defined as a varchar. In the linq query I need to compare that value to a datetime value (all of the values stored in the varchar column are valid dates). I'm trying this:

var list = (from o in session.Linq<ObjectName>() where Convert.ToDateTime(o.ColumnName) >= startDate select o).ToList();

When using Convert.ToDateTime I get this exception:

Cannot use subqueries on a criteria without a projection.

at NHibernate.Criterion.SubqueryExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters)
at NHibernate.Criterion.Junction.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)   
at NHibernate.Impl.CriteriaImpl.List()
at NHibernate.Linq.CriteriaResultReader`1.List()   
at NHibernate.Linq.CriteriaResultReader`1.<GetEnumerator>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)   
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

This is with 1.0.0.4000 of NHibernate.Linq.

+1  A: 

I would try mapping your varchar column as a DateTime, implementing a IUserType to do the conversion. I don't know how you're actually storing the DateTime (i.e. what representation), but keep in mind that the database has to be able to compare them the same way two datetimes compare. For example, ISO-8601 represented dates can do this.

Mauricio Scheffer
That worked. Thanks for the help.
Jeff