bug in select max of string in linq to nhibernate

Description

I have a Card class . This class has a Code column by string type.
I have a linq-to-NHibernte query for select max of codes.

var q = SessionInstance.Query<Card>()
.Max(x => x.Code);

For example data of this column is : `18950,9850` and expect result is `18950` and result is `9850`.

I changed this query to it :

var q = SessionInstance.Query<Card>()
.Max(x => int.Parse(x.Code));
But above query has a runtime exception by this message :

Expression type 'NhMaxExpression' is not supported by this SelectClauseVisitor.

Why?

Environment

None

Activity

Oskar Berggren January 15, 2013 at 1:03 PM

If you can submit the code within a few days it should be present in the 3.3.3 release.

In the meantime, you can use the same code with an official 3.3.2 build by just subclassing from DefaultLinqToHqlGeneratorsRegistry to add the registration and set the configuration property linqtohql.generatorsregistry to its assembly qualified name. (Or in you can use the Loquacious-configuration LinqToHqlGeneratorsRegistry<MyLinqToHqlGeneratorsRegistry>()

Oskar Berggren January 15, 2013 at 1:00 PM

Rather than spend time on a workaround, I think the quickest solution for you would be to simply add the support for ToInt32 and similar. I don't think it should be very difficult.

See for instance for some example:
https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Linq/Functions/MathGenerator.cs
The treeBuilder variable at the bottom has a Cast() method to output the corresponding HQL

Then it's need to be registered in
https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs

Ehsan Zeynali January 15, 2013 at 10:53 AM

By cast to nullable int , this problem is resolved. So my problem is other thing. My purpose of use to Parse() or ToInt32(), for this that cast string to int Before Max(). Is there another solution for my problem?

my expect query is :

select
max(cast(product0_.QuantityPerUnit as INT) as col_0_0_)
from
Products product0_

Oskar Berggren January 15, 2013 at 9:29 AM

For clarity, closing as not an issue - Max() over strings works correctly.

For supporting Parse() and ToInt32() etc, I've opened NH-3377.

Supporting these methods shouldn't be too difficult (inherit from BaseHqlGeneratorForMethod). Wanna have a go at creating a pull request to fix NH-3377?

Alex Zaytsev January 15, 2013 at 8:50 AM

string comparing works different way than numbers. If you will run your original query with linq-2-objects you will get the same result ("900" but not "80001")

Not an Issue

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created December 4, 2012 at 4:08 AM
Updated January 15, 2013 at 1:03 PM
Resolved January 15, 2013 at 9:29 AM
Who's Looking?