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!!
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:
Let’s assume your entities look something like this:
… 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:
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:
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).
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.