NHibernate Linq - how to create a where statement with IS NOT NULL
Asked Answered
C

1

7

how can i achieve this query with Nhibernate Linq?

var l = session.CreateQuery("from Auswahl a where a.Returnkey is not null").List<Auswahl>();

i tried this but it always returns an empty list.

var l = session.Linq<Auswahl>()
                   .Where(item => !String.IsNullOrEmpty(item.Returnkey))
                   .Select(item => item)
                   .ToList();
Calise answered 27/8, 2010 at 7:29 Comment(0)
O
7

Have you tried:

var l = session.Linq<Auswahl>()
                   .Where(item => item.Returnkey != null && item.Returnkey != "")
                   .Select(item => item)
                   .ToList();

I'm not sure that using String.IsNullOrEmpty would work, also it checks for two conditions - if it's NULL and if it's a blank empty string, how would that get translated into SQL? Might be worth having a look at SQL Profiler to see the raw SQL query it generates.

Occult answered 27/8, 2010 at 10:2 Comment(2)
thanks, you are right. but dont forget to write item.Returnkey != " " otherwise you get nothing from oracle. the Sql produced now looks like this: SELECT this_.ID as ID1_0_, this_.Programm as Programm1_0_, this_.Variante as Variante1_0_, this_.Returnkey as Returnkey1_0_, this_.Beschreibung as Beschrei5_1_0_ FROM AUSWAHL this_ WHERE (this_.Returnkey is not null and not (this_.Returnkey = ' ' /* :p0 */)) !there is a blank between ' ' :)Calise
It's woth remembering that, for Oracle, null and the empty string are the same.Lively

© 2022 - 2024 — McMap. All rights reserved.