2010-08-27 16 views
7

come posso ottenere questa query con Nhibernate Linq?NHibernate Linq - come creare un'istruzione where con IS NOT NULL

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

ho provato questo ma restituisce sempre una lista vuota.

var l = session.Linq<Auswahl>() 
        .Where(item => !String.IsNullOrEmpty(item.Returnkey)) 
        .Select(item => item) 
        .ToList(); 

risposta

7

Hai provato:

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

non sono sicuro che l'utilizzo String.IsNullOrEmpty avrebbe funzionato, anche verifica la presenza di due condizioni - se è NULL e se è una stringa vuota in bianco, come sarebbe tradotto in SQL? Potrebbe essere utile dare un'occhiata a SQL Profiler per vedere la query SQL raw che genera.

+0

grazie, hai ragione. ma non dimenticare di scrivere item.Returnkey! = "" altrimenti non ottieni nulla da Oracle. l'Sql prodotto appare come segue: SELEZIONA this_.ID come ID1_0_, this_.Programm come Programm1_0_, this_.Variante come Variante1_0_, this_.Returnkey come Returnkey1_0_, this_.Beschreibung come Beschrei5_1_0_ DA AUSWAHL this_ WHERE (this_.Returnkey non è null e non (this_.Returnkey = ''/*: p0 * /)) ! C'è uno spazio vuoto tra '' :) – blindmeis

+0

È da ricordare che, per Oracle, null e il la stringa vuota è la stessa. –