views:

42

answers:

2

Hi, I am currently working on a project using NHiberate as the DAL with .NET 2.0 and NHibernate 2.2.

Today I came to a point where I had to join a bunch of entities/collections to get what I want. That is fine.

What got me was that I do not want the query to return a list of objects of a certain entity type but rather the result would include various properties from different entities.

The following query is not what I am doing but it is kind of query that I am talking about here.

select order.id, sum(price.amount), count(item)
from Order as order
    join order.lineItems as item
    join item.product as product,
    Catalog as catalog
    join catalog.prices as price
where order.paid = false
    and order.customer = :customer
    and price.product = product
    and catalog.effectiveDate < sysdate
    and catalog.effectiveDate >= all (
        select cat.effectiveDate
        from Catalog as cat
        where cat.effectiveDate < sysdate
    )
group by order
having sum(price.amount) > :minAmount
order by sum(price.amount) desc

My question is, in this case what type result is supposed to be returned? It is certainly not of type Order, neither is of type LineItems.

Thanks for your help!

John

+2  A: 

you can always use List of object[] for returning data and it will work fine.

Fahad
Fahad, I'd appreciate it if you could be a bit more specific.The session.CreateQuery() has a list() method which I believe is the one I should use. How do you use the list() method to return array of objects that will include every item in my select statement?Thanks.
John
IQuery has a generic list method. you can call it as follows IQuery objQuery = mySession.CreateQuery(....... var result = objQuery.List<object[]>();
Fahad
Fahad, as I replied to Ben above, what you suggested does work. It was not working before due to the fact that I had SetResultTransformer(new DistinctRootEntityResultTransformer()) before calling List<object[]>(). Thanks.
John
+1  A: 

This is called a projection, and it happens any time you specify an explicit select clause that contains rows from various tables (or even aggregate / summary data from a single table).

Using LINQ you can create anonymous objects to store these rows of data, like this:

var crunchies = (from foo in bar
                where foo.baz == quux
                select new { foo.corge, foo.grault }).ToList();

Then you can do crunchies[0].corge for example to pull out the rows & columns.

If you are using NHibernate.Linq this will "just work".

If you're using HQL or Criteria API, then what Fahad mentioned will work. You'll get a List<object[]> as a result, and the index of the array references the order of the columns that you returned in your select clause.

Ben Scheirman
Ben, I wish I were using the version of .NET that supports LINQ. At this moment I have to reply HQL to get the result.You are right. What Fahad suggested does work. The reason it did not work before was because I had added SetResultTransformer(new DistinctRootEntityResultTransformer()) before I called List<object[]).Thank for your quick response!
John
Everything resolved then?
Ben Scheirman