Created
April 24, 2011 15:57
-
-
Save davybrion/939649 to your computer and use it in GitHub Desktop.
NHibernate QueryOver projection into DTO... anyone know of a cleaner way?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var orderHeaders = Session.CreateQuery( | |
@"select new OrderHeader(o.OrderedOn, c.Name, e.FirstName || ' ' || e.LastName) | |
from Order o inner join o.Customer c inner join o.Employee e") | |
.List<OrderHeader>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Customer customer = null; | |
Employee employee = null; | |
var orderHeaders = Session.QueryOver<Order>() | |
.JoinAlias(o => o.Customer, () => customer) | |
.JoinAlias(o => o.Employee, () => employee) | |
.Select(Projections.ProjectionList() | |
.Add(Projections.Property<Order>(o => o.OrderedOn)) | |
.Add(Projections.Property(() => customer.Name)) | |
.Add(Projections.Property(() => employee.FirstName)) | |
.Add(Projections.Property(() => employee.LastName))) | |
.List<Object[]>() | |
// the following select is the .NET Select extension method on IEnumerable, not part of the QueryOver api | |
.Select(values => new OrderHeader((DateTime)values[0], (string)values[1], (string)values[2] + " " + (string)values[3])); |
i prefer to stick with the constructor so the AliasToBeanConstructor approach might work... but i was hoping to get rid of the variable declarations for the aliases and to get NHibernate to do the string concatenation like it does with the HQL example
I guess you can do the string concatenation in the Select().
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure if it will work, but try this:
If you want to keep using the constructor for OrderHeader, I guess you can use the AliasToBeanConstructor transformer.