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 request returns 0 items, even though it is supposed to return one.
The following is a rewrite of the latter but with a call to the ToList()
method. This request works and returns the item 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 generic Linq to Nhibernate method with the boolean attribute determining if the request must be executed in the cache (true) or the database (false). There is supposedly nothing wrong in this method as it works normally in many other parts of the solution. The mapping of Item is nothing fancy: no bags and the following parameters:
lazy="false" schema="dbo" mutable="false" polymorphism="explicit"
Why is this so?
Edit:
The generated sql request of requestNoWorking ends with :
(Item.Group_ID is not null) and Item.Group_ID=@p0',N'@p0 int',@p0=11768
The generated sql request of requestWorking is roughly a select * from dbo.Items
requestWorking
you are reading EVERYItem
in the database and doing thewhere
query in memory on the results... – Jaquelinejaquelyni.Group != null
is not needed. (in linq to objects it is however) – Marlyni.Group.Id == methodParameter
will use whatever collation is set on the server while the in memory query will useEquals
on the object. (might not be the same at all) – Marlyn