QueryOver: select columns from subquery

Philipp

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 class MyModel
{
    public virtual int Id {get; set;}
    //more mapped values
    ....
    //unmapped values
    public virtual string ValueFromOtherTable {get;set;}
}

And I want to fill ValueFromOtherTable with a left join like this:

Select mt.*, ..., ot.ValueFromOtherTable from MyModelTable mt left 
join OtherTable ot ON (somecondition)

where MyModelTable is the table mapped to MyModel-class. I want to fill ValueFromOtherTable (no NH-mapping) by selecting all values from mt (to fill the NH-mapped columns) and then by using OtherTable I want to fill ValueFromOtherTable.

I can´t join both tables via QueryOver as there exists no direct parent-child correlation in the model, so JoinAlias or JoinQueryOver won´t work. My MainQueryOver queries MyModelTable.

ALTERNATIVE:

The alternative is to first get all values from MyModelTable and then using the properties there to query OtherTable. However this will result in an SELECT N+1 problem (for each model from MyModel select some OtherTable...) and also makes the code very complicated.

Is there a good way to solve this problem or is the only way to fill MyModel by using described alternative ?

Radim Köhler

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select multiple columns from a subquery in SQL Server

From Dev

Select matching to two columns of subquery

From Dev

Get columns from Tables (Using Inner Join) with DetachedCriteria (prefereable) or QueryOver

From Dev

Referencing FROM subquery in SELECT clause

From Dev

MYSQL Update from a select subquery

From Dev

Referencing FROM subquery in SELECT clause

From Dev

Mysql select from subquery is not working

From Dev

Select column(s) from subquery

From Dev

Is it possible to avoid subquery in select when concatenating columns?

From Dev

QueryOver: select ... where property in (...)

From Dev

NH QueryOver - use properties of main query in subquery

From Dev

Nhibernate Queryover with subquery get Next free number

From Dev

Returning multiple aggregated columns from Subquery

From Dev

Subquery to return multiple columns from newest record?

From Dev

Return multiple rows from multi SELECT subquery

From Dev

SELECT from subquery in Codeigniter Active Record

From Dev

MySQL SELECT latest record from a subquery with UNION

From Dev

T-SQL Select MAX from subquery

From Dev

Can I select from a destination table in a subquery?

From Dev

Subquery condition from main query select

From Dev

mysql subquery select with field from query

From Dev

Laravel query Builder Select from subquery

From Dev

MYSQL Select items in list from subquery

From Dev

How to use value from select in a subquery?

From Dev

Add a mysql subquery to select data from yesterday

From Dev

Oracle How to simplify a select count(*) from (subquery)?

From Dev

Select values from subquery without duplicates sql

From Dev

MySQL extracting columns from subquery to append it to main columns

From Dev

MySQL extracting columns from subquery to append it to main columns

Related Related

  1. 1

    Select multiple columns from a subquery in SQL Server

  2. 2

    Select matching to two columns of subquery

  3. 3

    Get columns from Tables (Using Inner Join) with DetachedCriteria (prefereable) or QueryOver

  4. 4

    Referencing FROM subquery in SELECT clause

  5. 5

    MYSQL Update from a select subquery

  6. 6

    Referencing FROM subquery in SELECT clause

  7. 7

    Mysql select from subquery is not working

  8. 8

    Select column(s) from subquery

  9. 9

    Is it possible to avoid subquery in select when concatenating columns?

  10. 10

    QueryOver: select ... where property in (...)

  11. 11

    NH QueryOver - use properties of main query in subquery

  12. 12

    Nhibernate Queryover with subquery get Next free number

  13. 13

    Returning multiple aggregated columns from Subquery

  14. 14

    Subquery to return multiple columns from newest record?

  15. 15

    Return multiple rows from multi SELECT subquery

  16. 16

    SELECT from subquery in Codeigniter Active Record

  17. 17

    MySQL SELECT latest record from a subquery with UNION

  18. 18

    T-SQL Select MAX from subquery

  19. 19

    Can I select from a destination table in a subquery?

  20. 20

    Subquery condition from main query select

  21. 21

    mysql subquery select with field from query

  22. 22

    Laravel query Builder Select from subquery

  23. 23

    MYSQL Select items in list from subquery

  24. 24

    How to use value from select in a subquery?

  25. 25

    Add a mysql subquery to select data from yesterday

  26. 26

    Oracle How to simplify a select count(*) from (subquery)?

  27. 27

    Select values from subquery without duplicates sql

  28. 28

    MySQL extracting columns from subquery to append it to main columns

  29. 29

    MySQL extracting columns from subquery to append it to main columns

HotTag

Archive