this is a follow-up to question Better DateTime? or use default(DateTime) for NULL?
I tried to implement a simple query that works with DateTime?
with a different example rather than the one of the other question.
In my case I have public virtual DateTime? Expiration
property mapped as
<property name="Expiration" not-null="false" type="DateTime" />
Now my LINQ query is the following
return (from product in session.Query<Product>()
where
!product.Hidden &&
(product.Expiration != null ||
(product.Expiration.Value - DateTime.Now).TotalDays < 5 && (product.Expiration.Value - DateTime.Now).TotalDays >= 0)
select product).ToList();
Query says: pick all products that are not hidden and not expiring in 5 days (<5 verify expired products so I have to add the second check)
I get an exception from ANTLR:
'Antlr.Runtime.NoViableAltException'. [.Where(NHibernate.Linq.NhQueryable`1[Model.Product], Quote((product, ) => (AndAlso(Not(product.Hidden), OrElse(DateTime.op_Inequality(product.Expiration, NULL), AndAlso(LessThan(DateTime.op_Subtraction(product.Expiration.Value, p2).TotalDays, p3), GreaterThanOrEqual(DateTime.op_Subtraction(product.Expiration.Value, p4).TotalDays, p5)))))), )]
How do I handle a DateTime? in NHibernate 3.0 with LINQ?