In Linq, Where clause is ignored if followed by Fetch and then by OrderBy

Description

When using Query<T>, Where clause followed by Fetch and then by OrderBy produces incorrect results by failing to include where predicate into the SQL at all.
Problem does not appear if removing Fetch or OrderBy from the sequence.

Erroneous behavior can be fixed by moving Where after Fetch, but please note that this workaround is not always possible; for example, if query including filtering is created at upper level of a call tree and then being processed by adding fetch/order in methods knowing nothing about where given IQueryable came from. Even throwing would be better than silently returning incorrect data, as it prompts to check each and every query, and correct one can be easily made incorrect by adding fetching or ordering.

For example, query like this
var contacts = session.Query<Contact>()
.Where(c => c.Id != contact.Id)
.Fetch(c => c.Profile)
.OrderBy(c => c.Id)
.ToList();

produces the following SQL without any trace of filtering by contact id:

select
contact0_.Id as Id0_0_,
profile1_.Id as Id1_1_,
contact0_.Profile as Profile0_0_
from
Contact contact0_
left outer join
Profile profile1_
on contact0_.Profile=profile1_.Id
order by
contact0_.Id asc

I will attach a failing test case to this issue.

Environment

None

Activity

Show:
Alex Zaytsev
June 4, 2012, 8:00 PM

Fix commited to master 6195424ffa3f1cb884a6983e847e26dcb45f4859

Alex Zaytsev
June 3, 2012, 6:31 AM

Are there any semantic differences between

var contacts = session.Query<Contact>()
.Where(c => c.Id != contact.Id)
.Fetch(c => c.Profile)
.OrderBy(c => c.Id)
.ToList();

and

var contacts = session.Query<Contact>()
.Where(c => c.Id != contact.Id)
.OrderBy(c => c.Id)
.Fetch(c => c.Profile)
.ToList();

o
October 18, 2011, 8:37 PM

Test case reproducing bug

Fixed

Assignee

Alex Zaytsev

Reporter

o

Components

Fix versions

Affects versions

Priority

Major