At What Level?
The perennial question.
- What is the lowest level with reasonable access to all that information?
- What is the highest level where such an implementation isn't too detailed?
Generally that would be the repository as it stores all that information, and is usually defined in terms of relational logic. So it should fit right in. Personally that is where I would place it.
However, the repository isn't necessarily the best fit. If for example this application provides users with the ability to specify queries for reporting, then relational logic is also apart of the domain. In such a case it may be more reasonable to place this knowledge inside the domain layer for easier articulation.
Another way to look at this is to observe the shearing forces in the application. Ideally you want code that is subjected to the same shearing force to be in the same location.
For example the particular data mapping frame work you are using will eventually be decommissioned, or receive a breaking change. In either case you will have to change/review all code associated with the framework.
If that code is consolidated in a single cohesive block, it is trivial to find, and update, or replace.
If it is peppered throughout the code base... ... ... The brochure saying it will be fun isn't accurate.
The same goes for every library/framework/language/operating system used or targeted by your code. Some of these things are in constant flux (looking at those frameworks) while others are so long lived (C) that they are practically guaranteed to exist.
Applying this to your query. If you chose to implement this using the data mapping framework directly then it definitely lives with all of the other code dealing with the same details. This will probably be the repository.
If you choose to use the language based implementation (LINQ). It probably shouldn't live inside the repository simple because its implementation details aren't in common - it has a different shearing force. A better place for it to live would be within the domain service/application method, depending on other concerns like security and access to data.