简体   繁体   中英

How to do a case-insensitive string where in NHibernate Linq query?

How to do a case-insensitive where in NHibernate Linq query?

eg

//note this one doesn't work if the entry in database has lowercase
q => q.Where(entity => 
    entity.CaseInsensitiveField == DesiredField.Trim().ToUpper())

Try this:

q => q.Where(entity => entity.CaseInsensitiveField.ToUpper() == DesiredField.Trim().ToUpper())

Also, I would suggest to set the parameter outside the query:

var value = DesiredField.Trim().ToUpper();

...

q => q.Where(entity => entity.CaseInsensitiveField.ToUpper() == value)

Use this:

q => q.Where(entity => 
    String.Equals(entity.CaseInsensitiveField , CaseInsensitiveField , 
                  StringComparison.OrdinalIgnoreCase));

UPDATE

It appears (at least via LinqPad) that the above will not translate into SQL, so I would suggest, as ivowiblo has already suggested:

var comparisonValue = CaseInsensitiveField.ToUpper();
q => q.Where(entity => 
    entity.CaseInsensitiveField.ToUpper() == comparisonValue);

The reason to set the ToUpper beforehand is that procedural functions are generally worse in SQL, and since this is not a database field, we can send it in already capitalized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM