QueryOver: select columns from subquery

前端 未结 1 1559
日久生厌
日久生厌 2021-01-20 20:33

How can I select / project values from a subquery from a different table into my main query?

I have an NH-model like this:

[Serializable]
public clas         


        
相关标签:
1条回答
  • 2021-01-20 20:49

    One way would be to use Projections, Subquery and DTO. So let's say, that we have DTO (almost the same as MyModel, but with new extern property ... e.g. Count). Then we can do it like this:

    MyModel main = null;
    MyModelDTO dto = null;
    
    // the main query
    var query = session.QueryOver<MyModel>(() => main);
    
    // the subquery used for projection
    var subquery = QueryOver.Of<OtherModel>()
        // select something, e.g. count of the ID
        .SelectList(selectGroup => selectGroup.SelectCount(o => o.ID))
        // some condition
        // kind of JOIN inside of the subquery
        .Where(o => o.xxx == main.yyy); // just example
    
    // now select the properties from main MyModel and one from the subquery
    query.SelectList(sl => sl
          .SelectSubQuery(subquery)
             .WithAlias(() => dto.Count)
          .Select(() => main.ID)
            .WithAlias(() => dto .ID)
          ....
        );
    
    // we have to use transformer
    query.TransformUsing(Transformers.AliasToBean<MyModelDTO >())
    
    // and we can get a list of DTO
    var list = query.List<MyModelDTO>();
    
    0 讨论(0)
提交回复
热议问题