views:

45

answers:

1

Hello,

Using nHibernate, I would like to query on an integer datatype, but its always returning the exact match.

How could I write an expression that returns a list starting with the number entered?

right now I am using it as: (clientNum is a long)

crit.Add(Expression.Like("ClientNumber", clientNum)); //this always gives me exact matches only

so I tried the following, but its complainging of a wroing type (its only expecting a string)

crit.Add(Expression.Like("ClientNumber", clientNum, MatchMode.Start));

Update: Also I tried the clientNum.ToString() but I get a db exception saying invalid type.

I can use the sql as follows to get what I want, but how do I do this in nHibernate??

SELECT * FROM ClientTable
WHERE clientNum LIKE '3%' --incase I wanted a list that starts with 3...
+1  A: 

I am not sure about the .NET version, but hybernate supported limited casting (e.g. cast (... as ...)) in HQL. I think you would need to cast the value to a string and then apply your 'Like' clause. Should be able to do this all in HQL. Alternatively, you can do it using the SQL Expressions (ex. Expression.Sql(...)) and do the same thing using T-SQL.

JoeGeeky
Something like `from Client where cast(clientNum as string) like '3%'`
Jamie Ide
@Jamie, I am not sure how to use your example, is it with Expression or Restriction or sql?Besides the number I want to query is not hardcoded it is passed into the method as a param., what do I do then?
VoodooChild
It is an Expression. You can build the actual sql string in the Expression dynamically... For Example `Expression.Sql(new SqlString(string.format("cast(clientNum as string) like '{0}%'", param)))`. Check the overloads and use the one that fits your needs.
JoeGeeky
Expression.Sql(new SqlString(string.format("clientNum like '{0}%'" -did the trick for me. clientNum is stored as a numeric on the db. Thanks.
VoodooChild