Ehsan
Ehsan

Reputation: 3491

How select max of string in linq to nhibernate

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?

Upvotes: 2

Views: 689

Answers (2)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

NHibernate doesn't know how to convert your int.Parse call to SQL.

Easiest solution: use SQL.

More complex and/or fun: extend the NHibernate LINQ provider to support that call. Google extending nhibernate linq for some links.

Upvotes: 1

TwTw
TwTw

Reputation: 547

Try to use Convert.ToInt32 instead of int.Parse . Take a look here: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/6c8892db-8df7-4174-b9b0-764dc1df82ad

Upvotes: 0

Related Questions