NHibernate is an Object/Relational Mapper, so to properly ask a "how do I write this query" question, you need to provide enough information so that potential answerers can understand what these three things look like:
- NHibernate entity classes (this is the Object part)
- Database tables (this is the Relational part)
- Mappings, whether they be HBM.XML, FluentNH, etc.
Let's assume your entities look something like this:
public class AccountProfile
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<NewsFeed> NewsFeeds { get; set; }
/// <summary>
/// People following me.
/// </summary>
public virtual IList<AccountProfile> Followers { get; set; }
/// <summary>
/// People I am following.
/// </summary>
public virtual IList<AccountProfile> Following { get; set; }
}
public class NewsFeed
{
public virtual int Id { get; set; }
public virtual AccountProfile AccountProfile { get; set; }
public virtual string Name { get; set; }
}
... where Followers and Following are opposite sides of a many-to-many relationship. This should be enough info for most people to be able to fill in the blanks and intuitively understand what the tables and mappings look like.
Now that we have that foundation, let's work on the query. Your query will be a bit easier to deal with if we rewrite it to use a join instead of a subquery:
select newsfeed.*
from
newsfeed
inner join follow
on newsfeed.AccountProfileID = follow.FollowerID
where follow.AccountProfileID='1'
This query should be exactly equivalent in behavior to the original query, more in-line with a relational database mindset, and perhaps a bit more efficient.
The equivalent NHibernate QueryOver (my preferred query API) query would look like this:
AccountProfile accountProfileAlias = null;
AccountProfile followingAlias = null;
var feeds = session.QueryOver<NewsFeed>()
.JoinAlias(x => x.AccountProfile, () => accountProfileAlias)
.JoinAlias(() => accountProfileAlias.Following, () => followingAlias)
.Where(() => followingAlias.Id == 1)
.List();
Let's translate this query into English: "Select the list of newsfeeds belonging to all the people who are following account #1." However, I have a feeling you meant "Select the list of newsfeeds belonging to all the accounts followed by account #1", which seems to me to be a bit more useful. In QueryOver, that looks like this (replace Following with Followers).
AccountProfile accountProfileAlias = null;
AccountProfile followerAlias = null;
var feeds = session.QueryOver<NewsFeed>()
.JoinAlias(x => x.AccountProfile, () => accountProfileAlias)
.JoinAlias(() => accountProfileAlias.Followers, () => followerAlias)
.Where(() => followerAlias.Id == 1)
.List();
If someone else wants to take a stab at this using NHibernate's LINQ provider, a.k.a. session.Query, feel free, but I personally don't have as much experience with that API.
var result = from n in session.Query<NewsFeed>() where (from f in session.Query<Follower>() where f.AccountProfile.Id == 1 select f.Follower).Contains(n.AccountProfile) select n;NewsFeed,AccountProfile, and (if it exists)Followclasses? If we understand how the entities relate to one another, we can better help you with your query.