QueryOver: select columns from subquery

前端 未结 1 1560
日久生厌
日久生厌 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(() => main);
    
    // the subquery used for projection
    var subquery = QueryOver.Of()
        // 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())
    
    // and we can get a list of DTO
    var list = query.List();
    

    0 讨论(0)
提交回复
热议问题