3

I need some help to write some queries. For this sql query

(select * from NewsFeed where ID=10)

nHibernate query is

var set = sesssion.Query<NewsFeed>().Where(c => c.ID == 10).ToList();

These are queries I typically used. Now I want to write some complex query like this.

Does anyone know to write a query using Nhibernate Query for this sql query

select *
from  newsfeed
where AccountProfileID in 
       (select follow.FollowerID
       from follow
       where follow.AccountProfileID='1')

I am using .net C# and Mysql

please help!!

2
  • 1
    does this do it 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; Commented Oct 14, 2011 at 11:44
  • I'm having a little difficulty picturing what your NHibernate entities look like. Could you please post some of the code for the NewsFeed, AccountProfile, and (if it exists) Follow classes? If we understand how the entities relate to one another, we can better help you with your query. Commented Oct 20, 2011 at 21:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.