如何编写 Nhibernate 查询 C# .net Mysql

发布于 2024-12-09 19:53:58 字数 528 浏览 0 评论 0原文

我需要一些帮助来编写一些查询。 对于这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-12-16 19:53:58

NHibernate 是一个对象/关系映射器,因此要正确询问“如何编写此查询”问题,您需要提供足够的信息,以便潜在的回答者能够理解这三个东西的样子:

  • NHibernate 实体类(这是 < b>对象部分)
  • 数据库表(这是关系部分)
  • 映射,无论它们是HBM.XML、FluentNH等。

让我们假设您的实体看起来某物像这样:

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; }
}

...其中追随者和关注者是多对多关系的相反双方。对于大多数人来说,这应该是足够的信息,能够填补空白并直观地理解表格和映射的样子。

现在我们已经有了这个基础,让我们开始查询。如果我们重写它以使用联接而不是子查询,您的查询将更容易处理:

select newsfeed.*
from
    newsfeed
    inner join follow
        on newsfeed.AccountProfileID = follow.FollowerID
where follow.AccountProfileID='1'

此查询在行为上应该与原始查询完全相同,更符合关系数据库思维方式,也许还有一点更有效率。

等效的 NHibernate QueryOver(我的首选查询 API)查询如下所示:

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();

让我们将此查询翻译成英语:“选择属于关注帐户 #1 的所有人员的新闻源列表。” 但是,我有一种感觉,你的意思是“选择属于帐户 #1 后面的所有帐户的新闻源列表”,在我看来,这更有用一些。在 QueryOver 中,看起来像这样(将 Follows 替换为 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();

如果其他人想使用 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:

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文