1 Comments

When using NHibernate (v. 2.1.2) with Linq to NHibernate (v. 1.1.0) you may encounter some problems if you use VB.NET as your coding language. Something as simple as the following example will throw a System.ArgumentException:

<span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">Dim</span> customers = From c <span style="color: #0000ff">in</span> Customer.Queryable _

<span id="lnum2" style="color: #606060">   2:</span>         Where c.FirstName = searchCrit.FirstName _

<span id="lnum3" style="color: #606060">   3:</span>         <span style="color: #0000ff">Select</span> c

The full exception is: System.ArgumentException : Expression of type 'System.Int32' cannot be used for return type 'System.Boolean’

This is because the Linq to NHibernate doesn’t handle the string comparisons correctly. Or VB.NET doesn’t. To fix this, you have to replace the = operator comparison with a call to Equals:

<span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">Dim</span> customers = From c <span style="color: #0000ff">in</span> Customer.Queryable _

<span id="lnum2" style="color: #606060">   2:</span>         Where c.FirstName.Equals(searchCrit.FirstName) _

<span id="lnum3" style="color: #606060">   3:</span>         <span style="color: #0000ff">Select</span> c

 

Thanks to @pettys for posting a helpful post on this subject.