Thanks Richard for your response.  If the NH dev team recommendation
is to use linq to cast the object array to the intended object type, I
feel this is a unacceptable and sloppy solution.  Having the API
return an object array, when it *knows* the type of object makes me
feel like I'm using an API from 2003.  Then having to box each
property and map them manually is tedious and error prone.  I guess I
fail to see why queries with the QueryOver api and projections
wouldn't use the same method that NH uses to populate full objects.
If this is the intended behavior, then I consider this a bug, even if
it is a non-functional bug.

Also, I did try to use a ResultTransformer, but was unsuccessful:

Session.QueryOver<Tenant>()
                                .Select(x => x.Id, x => x.FirstName, x
=> x.LastName)
                                
.UnderlyingCriteria.SetResultTransformer(Transformers.AliasToBean(typeof(Tenant)))
                                .List<Tenant>()

Is there a better way of doing this so that it works?

Thanks,

Jim



On May 8, 3:44 am, "Richard Brown \(gmail\)" <[email protected]>
wrote:
> Hi Jim,
>
> QueryOver returns an object[] in this case (like ICriteria which it is built
> on).  I think your options are to use a ResultTransformer (which looking at
> it is missing from the IQueryOver interface, but can be added to the
> UnderlyingCriteria), or use an extension method from the LINQ namespace:
>
> var tenants =
>     Session.QueryOver<Tenant>()
>         .Select(
>             x => x.Id,
>             x => x.FirstName,
>             x => x.LastName)
>         .List<object[]>()
>         .Select(properties => new Tenant() {
>             Id = (int)properties[0],
>             FirstName = (string)properties[1],
>             LastName = (string)properties[2],
>             });
>
> Richard
>
> --------------------------------------------------
> From: "Jim Geurts" <[email protected]>
> Sent: Friday, May 07, 2010 5:43 PM
> To: "nhusers" <[email protected]>
> Subject: [nhusers] Error using projections and a result collection
>
>
>
>
>
> > Hi all,
>
> > I'm trying to use projections to only return a few properties of my
> > domain object from the db.  The code I'm using looks like:
>
> > var tenants = Session.QueryOver<Tenant>()
> > .Select(x => x.Id, x => x.FirstName, x => x.LastName)
> > .List<Tenant>();
>
> > The generated sql is spot on, but unfortunately, things are not
> > working when NHibernate tries to map the result to an
> > IEnumerable<Tenant>.  It throws an exception similar to:
>
> > The value "System.Object[]" is not of type "MyDomain.Tenant" and
> > cannot be used in this generic collection.
> > Parameter name: value
> > System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value,
> > Type targetType) +122
>
> > System.Collections.Generic.List`1.System.Collections.IList.Add(Object
> > item) +150
> >   NHibernate.Util.ArrayHelper.AddAll(IList to, IList from) in d:
> > \Builds\NHibernate\nhibernate\src\NHibernate\Util\ArrayHelper.cs:201
> >   NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList
> > results) in d:\Builds\NHibernate\nhibernate\src\NHibernate\Impl
> > \SessionImpl.cs:1994
>
> > I've tried to set a transformer
> > (using
> > .UnderlyingCriteria.SetResultTransformer(Transformers.AliasToBean(typeof(Te 
> > nant)))),
> > but it did not seem to help.  This is using NHibernate 3.0.0.1001
>
> > Any ideas what is going on and how to fix it?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/nhusers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to