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(Tenant)))),
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 at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to