如何编写 Nhibernate 查询 C# .net Mysql
我需要一些帮助来编写一些查询。 对于这个 sql 查询,
(select * from NewsFeed where ID=10)
nHibernate 查询是
var set = sesssion.Query<NewsFeed>().Where(c => c.ID == 10).ToList();
这些是我通常使用的查询。现在我想写一些像这样的复杂查询。
有谁知道使用 Nhibernate Query 为这个 sql 查询编写一个查询
select *
from newsfeed
where AccountProfileID in
(select follow.FollowerID
from follow
where follow.AccountProfileID='1')
我正在使用 .net C# 和 Mysql
请帮助!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NHibernate 是一个对象/关系映射器,因此要正确询问“如何编写此查询”问题,您需要提供足够的信息,以便潜在的回答者能够理解这三个东西的样子:
让我们假设您的实体看起来某物像这样:
...其中追随者和关注者是多对多关系的相反双方。对于大多数人来说,这应该是足够的信息,能够填补空白并直观地理解表格和映射的样子。
现在我们已经有了这个基础,让我们开始查询。如果我们重写它以使用联接而不是子查询,您的查询将更容易处理:
此查询在行为上应该与原始查询完全相同,更符合关系数据库思维方式,也许还有一点更有效率。
等效的 NHibernate QueryOver(我的首选查询 API)查询如下所示:
让我们将此查询翻译成英语:“选择属于关注帐户 #1 的所有人员的新闻源列表。” 但是,我有一种感觉,你的意思是“选择属于帐户 #1 后面的所有帐户的新闻源列表”,在我看来,这更有用一些。在 QueryOver 中,看起来像这样(将 Follows 替换为 Followers)。
如果其他人想使用 NHibernate 的 LINQ 提供程序(又名
session.Query
)尝试一下,请随意,但我个人对该 API 没有太多经验。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.