views:

20

answers:

1

Why would the following work:

var profiles = (eventId > 0)
    ? Profiles.Query().Where(p => p.Event.Id == eventId).ToList()
    : Profiles.Query().Where(p => p.Event == null).ToList();

when the following will not:

var profiles = (from p in Profiles.Query()
                where (eventId > 0)
                    ? p.Event.Id == eventId
                    : p.Event == null
                select p).ToList()

The former returns 1 row when eventId is -1, but the latter returns 0 rows. I ran a SQL Profiler and found that NHibernate is generating a where clause that looks like:

WHERE 1 = 0 AND (...)

but I don't understand why? Does Linq to NHibernate have trouble evaluating a ternary expression in the where clause? Is this a problem with Linq in general?

A: 

It seems to be just in Linq to NHibernate. If you apply this query with Linq to Objects, it works fine.

AntonioR