1

我正在使用 Fluent-Nhibernate 1.3 版,并且正在尝试进行包含 5 个表的查询。我为 oracle 数据库创建了一个 sql 查询,并尝试使用 linq-to-nhibernate 进行复制。

遵循我的实体和映射示例。

实体:

public class A
{
    public virtual int idA { get; set; }
    public virtual String codA { get; set; }
    public virtual String tipoA { get; set; }
    public virtual IList<B> listB { get; set; }
}

public class B
{
    public virtual C objectC { get; set; }
    public virtual A objectA { get; set; }
    public virtual DateTime dtBegin { get; set; }
    public virtual DateTime dtEnd { get; set; }
}

public class C
{
    public virtual int idC { get; set; }
    public virtual String codeC { get; set; }
    public virtual IList<B> listB { get; set; }
    public virtual IList<D> listD { get; set; }
}

public class D
{
    public virtual C objectC { get; set; }
    public virtual string flgD { get; set; }
    public virtual DateTime dtBegin { get; set; }
    public virtual DateTime dtEnd { get; set; }
    public virtual E objectE { get; set; }
}

public class E
{
    public virtual int idE { get; set; }
    public virtual String dsE { get; set; }
    public virtual DateTime dtBegin { get; set; }
    public virtual DateTime dtEnd { get; set; }
    public virtual IList<D> listD { get; set; }
}

我的映射:

class AMap : ClassMap<A>
{
    public AMap()
    {
        Table("A");

        Id(x => x.idA, "ID_A").GeneratedBy.Sequence("StringA");
        Map(x => x.tipoA, "TP_A");
        Map(x => x.codA, "CODE_A");
        HasMany(x => x.listB).Cascade.All().KeyColumn("ID_A");
    }
}

class BMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");

        CompositeId()
            .KeyReference(x => x.objectC, "ID_C")
            .KeyReference(x => x.objectA, "ID_A")
            .KeyProperty(x => x.dtBegin, "DT_BEGIN");

        Map(x => x.dtEnd, "DT_END");
    }
}

class CMap : ClassMap<C>
{
    public CMap()
    {
        Table("C");
        Id(x => x.idC, "ID_C").GeneratedBy.Sequence("StringC");
        Map(x => x.codeC, "CODE_C");
        HasMany(x => x.listB).Cascade.All().KeyColumn("ID_C");
        HasMany(x => x.listD).Cascade.All().KeyColumn("ID_D");
    }
}

class DMap : ClassMap<D>
{
    public DMap()
    {
        Table("D");

        CompositeId()
            .KeyReference(x => x.objectC, "ID_C")
            .KeyProperty(x => x.flgD, "FLG_D")
            .KeyProperty(x => x.dtBegin, "DT_BEGIN");
        References(x => x.objectE, "CODE_E");
        Map(x => x.dtEnd, "DT_END");
    }      
}

class EMap : ClassMap<E>
{
    public EMap()
    {
        Table("E");

        Id(i => i.idE, "ID_E").GeneratedBy.Assigned();
        Map(m => m.dsE, "DSC_E");
        Map(m => m.dtBegin, "DT_BEGIN");
        Map(m => m.dtEnd, "DT_END");
        HasMany(x => x.listD).Cascade.All().KeyColumn("ID_E");
    }
}

我的 SQL 查询(有效):

SELECT C.CODE_C, E.CODE_E, E.DT_BEGIN
FROM TABLEA A, TABLEB B, TABLEC C, TABLED D, TABLEE E
WHERE A.CODE_A = '0000' AND A.ID_A = B.ID_A AND B.ID_C = C.ID_C AND B.DT_END IS NULL
AND C.ID_C = D.ID_C AND D.DT_END IS NULL AND D.CODE_E = E.CODE_E AND E.DT_END IS NULL;

我尝试使用多重连接,但随后一些关系是集合,所以我必须在连接内创建一个位置。

所以我的问题是:是否可以进行与 linq-to-nhibernate 相同的 Sql 查询,或者更好地进行一系列选择?不,我不能更改数据库。

提前致谢。

4

1 回答 1

1

is null 查询只能使用 DateTime 吗?作为类型

var query = from b in B
            from c in b.C
            from d in c.listD
            from e in d.E
            where b.A.Code == "0000" && b.EndDate == null & ...
            select new { Ccode = c.Code, Ecode = e.Code, E_BeginDate = e.BeginDate }

更新:回答第三条评论

分组结果必须在内存中完成,因为 sql 中的分组只能返回聚合

var results = query.AsEnumerable()
    .GroupBy(a => a.Ccode, a => a.Ecode, (key, values) => new { Ccode = Key, Ecodes = values.ToList() })
    .List();
于 2012-09-19T13:55:12.560 回答