Reputation:
I have a Linq to NHibernate query as follows:
var profile =
from UserProfile up in _Session.Query<UserProfile>()
.Fetch(x=>x.Messages)
where up.UserName == userName
select up.Messages;
this returns an IQueryable<IList<UserMessage>>
which I then have to run a SelectMany()
on. I'd prefer if I could just return an IQueryable<UserMessage>
object instead, especially as the query will never return more than one user profile. Can this be done, or am I stuck with the extra step?
Upvotes: 2
Views: 833
Reputation: 139778
If you map the other side of the navigation e.g have a UserProfile
property on the UserMessage
class, your can start from UserMessage
:
var messages =
from UserMessage um in _Session.Query<UserMessage>()
where um.UserProfile.UserName == userName
select um;
Otherwise you need to use SelectMany()
to get a flattened out list.
Upvotes: 1
Reputation: 21764
Could you query the messages table directly and use the reverse association?
IQueryable<Message> messages = ...;
var filtered = from m in messages
where m.UserProfile.UserName == userName
select m;
Also, if you're willing to forgo query syntax you could make this shorter with:
var profile = _Session.Query<UserProfile>()
.Where(up => up.UserName == userName)
.SelectMany(up => up.Messages);
Upvotes: 1