I wanted to find out whether this is an NHibernate issue, or a LINQ issue 
before posting it as an issue in JIRA. This issue only occurs in VB.Net.

When running any query containing a nullable type in the where clause, an 
exception is thrown. For example:

Dim data = (From t In _session.Query(Of Table)()
                  Where t.IntegerField = 123).ToList()

Where IntegerField is a nullable integer, throws: 
Unable to cast object of type 'NHibernate.Hql.Ast.HqlCoalesce' to type 
'NHibernate.Hql.Ast.HqlBooleanExpression'.

The problem stems from the LINQ expression that is generated, in VB the 
where clause generates:
.Where(t => ((t.IntegerField == ConvertChecked(123)) ?? False))

Whereas in C# the equivalent querys where clause generates:
.Where(t => (t.IntegerField == Convert(123)))

Interestingly, if I use the alternative syntax:
Dim data = (From t In _session.Query(Of Table)()).Where(Function(t) 
(t.IntegerField = 123)).ToList()

the expression generated is:
.Where(t => Convert((t.IntegerField == ConvertChecked(123))))

Which works fine.

Someone else has raised this as an issue before (possibly with a better 
description!) here:
http://stackoverflow.com/questions/9518058/nhibernate-linq-error-querying-when-nullable-types-in-where-clause


Is this something that could / should be fixed in NHibernate or is it more 
of a VB LINQ compiler issue? Obviously we can work around it but it does 
make our code unnecessarily complex.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/7uro03_TkjUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to