Search code examples
c#nhibernatetype-conversionprojection

NHibernate Projections.Cast to custom type


My quantity column is an int. I want to use Like on it then i try to conver its type to string.

To do this i use:

 Expression.Like(
 Projections.Cast( NHibernateUtil.String,
 Projections.Property<ErrorSummaryEntity>(x => x.Quantity)),
 searchValue, MatchMode.Anywhere)

Its almost works. It generate sql like this.

 cast( this_.ILOSC as NVARCHAR2(255)) like :p1

Problem here is NVARCHAR. I need to get VARCHAR instead.

How can i get this?cast( this_.ILOSC as VARCHAR2(255)) like :p1

Second question that might help is: If i have mapping like this:

Map(x => x.Quantity) .Column("ILOSC")

Thats why i use lambda to work on this column. Is possible to retrieve maped column name?

var colName = //some action to return "ILOSC"

Projections.Property<ErrorSummaryEntity>(x => x.Quantity) gives me "Quantity". How can i get "ILOSC"?

If i get this column name i can use Expression.Sql().


Solution

  • You are almost there, just instead of NHibernateUtil.String we have to use: NHibernateUtil.AnsiString

    Expression.Like(
        // Projections.Cast( NHibernateUtil.String,
        Projections.Cast( NHibernateUtil.AnsiString,
        Projections.Property<ErrorSummaryEntity>(x => x.Quantity)),
            searchValue, MatchMode.Anywhere)
    

    See: 5.2.2. Basic value types