Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform conditional projection to boolean

Tags:

nhibernate

I have a simple case statement within my query that essentially says if the count of the subquery is 1 return false, else return true. The query generated by NHibernate is correct however I'm having a hard time getting the boolean return value of the case statement to transform into my dto.

Here is my restriction

var restriction = Restrictions.Eq(Projections.SubQuery(QueryOver.Of(() => alias)
    .Select(Projections.Count(() => alias.Id))
    .Where(x => x.AddedBy == UserProvider.Current.UserName)
    .Where(() => alias.Item.Id == alias2.Id)), 1);

And here is my projection

.Add(
    Projections.Conditional(restriction,
        Projections.Cast(NHibernateUtil.YesNo, Projections.Constant(false)),
        Projections.Cast(NHibernateUtil.YesNo, Projections.Constant(true))))
            .WithAlias(() => dto.CanApply))
)

Here, the CanApply property from my dto is a boolean. The error I receive is {"The type System.Int32 can not be assigned to a property of type System.Boolean setter of Dto.CanApply"}.

I've tried everything I can think of to try and make NHibernate transform an integer to boolean.

What am I missing?

Edit

I was able to resolve this by creating a custom result transformer by inheriting IResultTransformer and checking each aliases to see if it's the one causing the issue. Once I found the culprit alias I simply issue a Convert.ToBoolean(tupleValue) and call it a day.

Can anyone please confirm whether I'm re-inventing the wheel or does NHibernate have a built in mechanism that I'm missing for doing such thing?

like image 695
Nosila Avatar asked Jul 21 '26 21:07

Nosila


1 Answers

Try specifying the type in Projections.Constant. Like this:

.Add(
 Projections.Conditional(restriction,
    Projections.Constant(false, NHibernateUtil.Boolean)),
    Projections.Constant(true, NHibernateUtil.Boolean))))
        .WithAlias(() => dto.CanApply))
 )
like image 179
Samuel Jack Avatar answered Jul 25 '26 05:07

Samuel Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!