Using NHibernate QueryOver with a complex scenario

杀马特。学长 韩版系。学妹 提交于 2019-12-03 21:30:56

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