Question

I am current having an issue with getting records from database using nhibernate.

My 'publicstring' column is having two values, one with lower case and the other with upper case. I am trying to fetch one of them depending on what is typed.

Below is my query

 private string _publicId;
 var _mediaFileShare = this.Session.QueryOver<MediaFileShare>()
                                   .Where(q => q.Publicsting == _publicstring)
                                   .SingleOrDefault();

Answers would be appreciated..

Was it helpful?

Solution

A naive solution is:

var _mediaFileShare = this.Session.QueryOver<MediaFileShare>()
    .Where(q => q.PublicString == _publicString)
    .List()
    .SingleOrDefault(r => string.Compare(r.PublicString, _publicString, StringComparison.Ordinal) == 0);

If you're confident that the query will always return two rows then performance should be very acceptable. Alternatively you could use a dynamic order by, something like:

    var query = this.Session.QueryOver<MediaFileShare>()
        .Where(q => q.PublicString == _publicString);

    query = _publicString == _publicString.ToLower() 
        ? query.OrderBy(q => q.PublicString) 
        : query.OrderByDescending(q => q.PublicString);

    var _mediaFileShare = query.Take(1).SingleOrDefault();

The results using an order by will be dependent on your database and its collation setting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top