Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:08:34+00:00 2026-06-12T01:08:34+00:00

I hit a situation today where a field in our legacy db that should

  • 0

I hit a situation today where a field in our legacy db that should never be empty… was empty.

I am using NHibernate 3.2 against this database and the queries that are affected are written in QueryOver.

My current query is this

    return Session
        .QueryOver<FacilityGroup>()
        .Where(fg => fg.Owner.Id == Token.OwnerId && 
                     fg.UserName == Token.UserName)
        .OrderBy(fg => fg.Code).Asc
        .TransformUsing(Transformers.DistinctRootEntity);

I want it to be this:

        return Session
            .QueryOver<FacilityGroup>()
            .Where(fg => fg.Owner.Id == Token.OwnerId && 
                         fg.UserName == Token.UserName && 
                         !string.IsNullOrEmpty(fg.Code))                
            .OrderBy(fg => fg.Code).Asc
            .TransformUsing(Transformers.DistinctRootEntity);

When I try this I get an exception “Unrecognised method call: System.String:Boolean IsNullOrEmpty(System.String)”

So NHibernate can’t translate string.IsNullOrEmpty. Fair enough. However when I try this

        return Session
            .QueryOver<FacilityGroup>()
            .Where(fg => fg.Owner.Id == Token.OwnerId && 
                         fg.UserName == Token.UserName && 
                         !(fg.Code == null || fg.Code.Trim() == "" ))
            .OrderBy(fg => fg.Code).Asc
            .TransformUsing(Transformers.DistinctRootEntity);

I get an InvalidOperationException “variable ‘fg’ of type ‘Domain.Entities.FacilityGroup’ referenced from scope ”, but it is not defined”

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:08:35+00:00Added an answer on June 12, 2026 at 1:08 am

    Ok… I guess I asked this question too soon. I figured out a way around this.

    What I was able to do was invoke the ‘trim’ function from hql via a SQL Function Projection. I ended up writing it as IQueryOver Extention method to keep it flexible. I will post it here in case anyone needs it.

    public static class QueriesExtentions
    {
        public static IQueryOver<E, F> WhereStringIsNotNullOrEmpty<E, F>(this IQueryOver<E, F> query, Expression<Func<E, object>> propExpression)
        {
            var prop = Projections.Property(propExpression);
            var criteria = Restrictions.Or(Restrictions.IsNull(prop), Restrictions.Eq(Projections.SqlFunction("trim", NHibernateUtil.String, prop), ""));
            return query.Where(Restrictions.Not(criteria));
        }
    }
    

    and here it is in use

        return Session
            .QueryOver<FacilityGroup>()
            .Where(fg => fg.Owner.Id == Token.OwnerId && fg.UserName == Token.UserName )
            .WhereStringIsNotNullOrEmpty(fg => fg.Code)
            .OrderBy(fg => fg.Code).Asc
            .TransformUsing(Transformers.DistinctRootEntity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Situation I've been hit with a situation that I've never encountered before. I
I used to have a situation where I hit the database a every time
I'm in the same situation as this guy (only that my problem is with
I'm using EF 4.1 code first. Running into a very weird situation: Database does
I have this situation: An object that it's observable and another object that it
I am using a DB2 provider to hit a database on an AiX Server.
today i hit a really annoing problem with wpf. i just want to align
I seem to have hit a snag while building my project using Ant. The
Hi I want to hit one URL that show an image in half page
I was recently hit with a puzzling problem with no explanation that I somehow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.