Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Nhibernet linq中连接两个表查询并选择匹配记录?_C#_Linq_Nhibernate_Linq To Nhibernate_Webapi - Fatal编程技术网

C# 如何在Nhibernet linq中连接两个表查询并选择匹配记录?

C# 如何在Nhibernet linq中连接两个表查询并选择匹配记录?,c#,linq,nhibernate,linq-to-nhibernate,webapi,C#,Linq,Nhibernate,Linq To Nhibernate,Webapi,我正在尝试开发一个查询,它使用linq Nhibernet查询在一个结果中提供来自两个表的记录 我有两个表,第一个是account accountId,accountDescription,第二个是accountDescription历史accountId,description。AccountId是第二个表的外键引用。现在,我使用下面的查询从第一个表中获取所有记录 我想要的是,如果accountId引用的AccountDescriptionHistory中存在记录,那么它应该返回Account

我正在尝试开发一个查询,它使用linq Nhibernet查询在一个结果中提供来自两个表的记录

我有两个表,第一个是account accountId,accountDescription,第二个是accountDescription历史accountId,description。AccountId是第二个表的外键引用。现在,我使用下面的查询从第一个表中获取所有记录

我想要的是,如果accountId引用的AccountDescriptionHistory中存在记录,那么它应该返回AccountDescriptionHistory中的描述,而不是account表中的描述。我想在单个查询中实现这一点

注意:我在linq NHibernate查询中需要这个

添加类详细信息

帐户类别如下所示:

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

}
帐户说明类别:

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

您可以通过查询来实现这一点

        /* 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;
或以流畅的风格

            ////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();
我猜你的poco是这样的

public class Account
{
  /* scalars */

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


你好友好的建议:在问题中包含所有相关代码。为了解决问题,有时我们需要在本地运行代码。如果有帮助的话,我已经添加了一些细节。您仍然需要向我们提供所有的类,否则我们将不得不猜测您是如何实现的。我们都知道:假设是所有f..ups之母:-您需要通过属性或Fluent显示您的Orm Poco和映射。你需要发布文本代码,而不是截图代码。如果有帮助,我已经添加了一些课堂信息。你缺少一个选择,请查看我的答案。吼。嘿我没有错过选择。这是NHibernate hql,不是SQL。缺少选择意味着什么都可以得到。如果您按照我的答案中的链接进行操作,您也会在hql中看到no SELECT。->从订单o+内部连接获取o.OrderLines+内部连接获取o.Customer+,其中o.Id=:Id;嗨,格林纳达科德。很抱歉耽搁了,因为我们都做了很长时间的交易,我想在回应之前彻底测试一下。我的发现:如果不使用select,查询将返回一个对象列表,这正是我所期望的。通过简单的选择,它将返回一个包含重复项的结果。我已经用select distinct更新了我的答案。这在fetch中是不正确的,但在没有select的情况下是正确的:如果没有select,查询将返回一个对象列表,这是我所期望的。尽管如此,它仍然返回重复项。