将NHibernate QueryOver与复杂场景一起使用

时间:2012-03-08 16:21:35

标签: nhibernate queryover

我在这种情况下尝试使用QueryOver:

public class Class1
{
    public virtual string Name { get; set; }
    public virtual string Descripton { get; set; }
    public virtual Class2 { get; set; }
    public virtual IList<Class3> ListClass3{ get; set; }
    ... //SEVERAL OTHERS LISTS, PROPERTIES
}

 public class Class2
{
    public virtual string Name { get; set; }
    ... //SEVERAL OTHERS LISTS, PROPERTIES
}


public class Class3
{
    public virtual string Name { get; set; }
    public virtual Class4 { get; set; }
    ... //SEVERAL OTHERS LISTS, PROPERTIES
}

public class Class4
{
    public virtual string Prop1 { get; set; }
    public virtual string Prop2{ get; set; }
    ... //SEVERAL OTHERS LISTS, PROPERTIES
}

我的DTO:

public class ClassDTO
{
    public string NameClass1 { get; set; }
    public string DescriptonClass1 { get; set; }
    public string NameClass2 { get; set; }

    public virtual IList<Class3> Class3List { get; set; }       
}

我的问题是如何获得IList ......如果没有它,那到目前为止工作正常:

Class2 class2 = null;
IList<Class3> listClass3 = null;
var queryOver = Session.QueryOver<clsClass1>();

var list =  queryOver
            .JoinAlias(x => x.Class2, () => class2)
            .JoinAlias(x => x.ListClass3, () => listClass3, JoinType.LeftOuterJoin)
            .SelectList(list => list                
              .Select(c2 => c2.Name).WithAlias(() => myDTO.NameClass1)
              .Select(c2 => class2.Name).WithAlias(() => myDTO.NameClass2)
              //NEED GET LIST<CLASS3>
             )
       .TransformUsing(Transformers.AliasToBean<ClassDTO>())
            .List<ClassDTO>();

这很好,但我现在需要'填补'IList ......如果可能的话,只需从Class4中获取Prop1和Prop2 ......

由于

1 个答案:

答案 0 :(得分:1)

接近你想要进行一次往返的东西

// get ClassDTOs
Class2 class2 = null;
ClassDTO myDTO = null;
var results = Session.QueryOver<Class1>()
    .JoinAlias(x => x.Class2, () => class2)
    .SelectList(list => list                
        .Select(c1 => c1.Id).WithAlias(() => myDTO.IdClass1)
        .Select(c1 => c1.Name).WithAlias(() => myDTO.NameClass1)
        .Select(c1 => c1.Description).WithAlias(() => myDTO.DescriptionClass1)
        .Select(() => class2.Name).WithAlias(() => myDTO.NameClass2)
    )
    .TransformUsing(Transformers.AliasToBean<ClassDTO>())
    .Future<ClassDTO>();

// get Class3DTOs
Class3 class3 = null;
Class3DTO myClass3DTO = null;
var subresults = Session.QueryOver<Class1>()
    .JoinAlias(x => x.Class3List , () => class3)
    .JoinAlias(() => classe3.Class4 , () => class4)
    .SelectList(list => list                
        .Select(c => c.Id)
        .Select(() => class3.Name)
        .Select(() => class4.Prop1)
        .Select(() => class4.Prop2))
    .Future<object[]>()
    .ToLookup(array => (int)array[0], array => new myClass3DTO
    {
        NameClass3 = (string)array[1],
        Prop1Class4 = (string)array[2],
        Prop2Class4 = (string)array[3],
    });

// assigne the lists to the dto
foreach (var result in results)
{
    result.ListClass3 = subresults[result.IdClass1].ToList();
}

return results;