I have two similar queries theoretically returning the same results:
var requestNotWorking = SessionManagement.Db.Linq<Item>(false).Where(i => i.Group != null && i.Group.Id == methodParameter) .ToList();
This query returns 0 elements, although it should return one. The following is a ToList() latter, but with a call to the ToList() method. This query works and returns the element expected in the first query!
var requestWorking = SessionManagement.Db.Linq<Item>(false).ToList().Where(i => i.Group != null && i.Group.Id == methodParameter).ToList();
Note. SessionManagement.Db.Linq<Item>(false) is a common Linq to Nhibernate method with a boolean attribute that determines whether the request should be executed in the cache (true) or in the database (false). There is apparently nothing wrong with this method, since it works fine in many other parts of the solution. The element mapping has nothing to do: no packages and the following parameters: lazy="false" schema="dbo" mutable="false" polymorphism="explicit"
Why is this so?
Edit
The generated sql query requestNoWorking ends with:
(Item.Group_ID is not null) and Item.Group_ID=@p0',N'@p0 int',@p0=11768
The generated sql requestWorking query is approximately equal to select * from dbo.Items
Keysharpener
source share