Using NHibernate QueryOver with a complex scenario

…衆ロ難τιáo~ 提交于 2019-12-09 14:05:58

问题


I´m trying to use QueryOver in that scenario :

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
}

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

My problem is how get the IList ... Without that, thats working fine so far:

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>();

Thats working fine, but I need to 'fill' the IList now... And if possible, get just the Prop1 and Prop2 from Class4...

Thanks


回答1:


something that is close to what you want taking one roundtrip

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


来源:https://stackoverflow.com/questions/9621036/using-nhibernate-queryover-with-a-complex-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!