简体   繁体   English

如何连接两个表查询和 select 匹配 Nhibernet linq 中的记录?

[英]How to join two tables queries and select matching the records in Nhibernet linq?

I am trying to develop one single query which gives me records from both the table in one result using linq-Nhibernet query.我正在尝试开发一个单一的查询,它使用 linq-Nhibernet 查询在一个结果中为我提供两个表中的记录。

I have two tables first one:account (accountId, accountDescription) and second one: accountdescriptionhistory (accountId, description).我有两个表第一个:account(accountId,accountDescription)和第二个:accountdescriptionhistory(accountId,description)。 AccountId is foreign-key reference to second table. AccountId 是对第二个表的外键引用。 Now, i am fetching all the records from first table with below query.现在,我正在使用以下查询从第一个表中获取所有记录。

在此处输入图像描述

What i want is, If record exist in AccountDescriptionHistory for accountId reference than it should return me the description from AccountDescriptionHistory not from account table.我想要的是,如果 AccountDescriptionHistory 中存在用于 accountId 参考的记录,那么它应该返回来自 AccountDescriptionHistory 的描述而不是来自帐户表。 i want to achieve this in single query.我想在单个查询中实现这一点。

Note: I need this in linq NHibernate query.注意:我在 linq NHibernate 查询中需要这个。

Adding a class details添加 class 详细信息

Account class look like below:帐户 class 如下所示:

public class Account : EntityModelHasOwner<Account>, ISupportsIdLookup
{

    /// <summary>
    /// The account number
    /// </summary>
    public virtual string AccountNumber { get; set; }

    /// <summary>
    /// The account's description
    /// </summary>
    public virtual string Description { get; set; }

}

Account description class:账户说明 class:

public class AccountDescriptionHistory : EntityModel<AccountDescriptionHistory>
{
    #region Public Properties

    /// <summary>
    /// The account description of an account that is valid for a specific date range
    /// </summary>
    public virtual string AccountDescription { get; set; }

    /// <summary>
    /// The account this AccountDescriptionHistory is associated with.
    /// </summary>
    public virtual Account Account { get; set; }
}

You can accomplish this via a query.您可以通过查询来完成此操作。

        /* this.Session is a NHibernate.ISession */

        string hql = "FROM Account acc" +
             " inner join fetch acc.MyAccountDetails"
               "where acc.IsDeleted= :myIsDeletedParameter";
        IQuery q = this.Session.CreateQuery(hql);
        q.SetBoolean("myIsDeletedParameter", false);
        IList<Account> returnItems = q.List<Account>();
        return returnItems;

OR with a Fluent style;或流利的风格;

            ////using NHibernate;
            ////using NHibernate.Linq;

            IQueryable<Account> returnItemsQuery = (from myalias in this.Session.Query<Account>()
                .FetchMany(x => x.MyAccountDetails )
                .Where(acc => false == acc.IsDeleted)
                    select myalias);

            IList<Account> returnItems = returnItemsQuery.ToList();

I am assuming your poco looks like this.我假设你的 poco 看起来像这样。

public class Account
{
  /* scalars */

  public virtual ICollection<AccountDetails> MyAccountDetails {get; set;}
}

See:看:

https://nhibernate.info/doc/howto/various/lazy-loading-eager-loading https://nhibernate.info/doc/howto/various/lazy-loading-eager-loading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM