如何查询列表中的复合 id 键

How to query a composite id key inside a list

如何在多个 IN 子句中查询复合键字段 SQL?

我的子公司是字符串和公司的组合

public class Company
{
     public virtual String Id { get; set; }
}

public class Subsidiary
{
     public virtual String Id { get; set; }
     public virtual Company Company { get; set; }
}

public class Disponibility
{
     public virtual String IdCompany { get; set; }
     public virtual String IdSubsidiary { get; set; }
}

目前卡在这

var subsidiarys = session.Query<Subsidiary>().ToList();
var result = session.Query<Disponibility>().Where(x => subsidiarys.Contains(???) ).ToList();

生成的查询需要

SELECT * FROM VW_DISPONIBILITY D
WHERE (D.COMPANY, D.SUBSIDIARY) IN (SELECT S.COMPANY, S.SUBSIDIARY FROM SUBSIDIARY S);

并非所有数据库都支持 oracle multiple IN 子句,因此唯一的方法是连接多个 or

var disjunctions = Restrictions.Disjunction();
foreach (var sub in subsidiarys)
{
    disjunctions.Add(Restrictions.Where<Disponibility>(x => x.IdCompany == sub.Company.Id && x.IdSubsidiary == sub.Id));
}

var result = session.QueryOver<Disponibility>()
                    .Where(disjunctions)
                    .List<Disponibility>();

生成SQL

SELECT
    ...
FROM
    VW_DISPONIBILITY this_ 
WHERE
    (
        (
            this_.COMPANY = :p0 
            and this_.SUBSIDIARY = :p1
        ) 
        or (
            this_.COMPANY = :p2 
            and this_.SUBSIDIARY = :p3
        )
        ...
    );