C# fluent nhibernate在后期绑定时左连接,运行时值

C# fluent nhibernate在后期绑定时左连接,运行时值,c#,fluent-nhibernate,left-join,C#,Fluent Nhibernate,Left Join,我有一个映射了许多关系的类。 应该为具体用户获取其中一个关系 public class Company { public int id { get; set; } public string Name { get; set; } public IList<StocksPack> stocks { get; set; } // a lot of other properties referencing other objects } public cl

我有一个映射了许多关系的类。 应该为具体用户获取其中一个关系

public class Company
{
    public int id { get; set; }
    public string Name { get; set; }
    public IList<StocksPack> stocks { get; set; }
    // a lot of other properties referencing other objects
}

public class StocksPack
{
    public int stockId { get; set; }
    public int companyId { get; set; }
    public int amount { get; set; }
    public int ownerId { get; set; }
}
上市公司
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共IList存储{get;set;}
//许多其他属性引用其他对象
}
公营股票交易所
{
public int stockId{get;set;}
public int companyId{get;set;}
公共整数金额{get;set;}
public int ownerId{get;set;}
}
如何映射要左外连接的
库存
ON stocks.companyId=company.id和stocks.ownerId=123456

其中实际的
ownerId
只有在用户登录到系统后的运行时才知道。
我想为具体用户获取有关该公司的所有信息和该公司的
股票列表。

我认为你问题的前提是错误的:你没有在左外联接上映射对象。在数据库中使用外键引用或在nhibernate中使用类似于以下内容的

public class Company
{
    public int id { get; set; }
    public string Name { get; set; }
    public IList<StocksPack> stocks { get; set; }
    // a lot of other properties referencing other objects
}

public class StocksPack
{
    public int stockId { get; set; }
    public int companyId { get; set; }
    public int amount { get; set; }
    public int ownerId { get; set; }
}
public class StockeMap : ClassMap<Stock>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        References(x => x.company);
    }
}
你有一个公司对象,上面有一个地图,给它一个id字段。 这是您与股票和公司之间的简单父子id类型关系。我想是吧。 hibernate和nhibernate背后的思想是,对象中不必有id引用。它会帮你处理的

左侧外部联接仅在查询数据时输入。换句话说,映射是两种类型之间的关系,查询是关于如何表示该关系以及需要什么数据的语句。前者是数据模型和系统设计的属性,后者是在运行时发生的事件

您希望查询执行的操作有些含糊不清,但似乎您希望列出所有股票,并且当该股票由某个所有者拥有时,该股票的公司也要列出。另一种可能性是,你想列出所有的公司,当他们有一个特定id拥有的股票时,也要列出该股票

在这两种情况下,您都需要一个简单的左连接查询。在fluent bhibernate中,它们看起来像:

在C#使用linq时,您可以使用:


您可以使用名为“”的功能。如果您需要,我可以使用您提到的实体制作一个示例应用程序

from Cat as cat
    left join cat.kittens as kitten
var query = from person in people
                    join pet in pets on person equals pet.Owner into gj
                    from subpet in gj.DefaultIfEmpty()
                    select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };