problem in convert the IList into IList<T> object

989 views
Skip to first unread message

waruni

unread,
Mar 16, 2010, 3:48:30 AM3/16/10
to nhusers
hi

i have a sql statment and it will execute using CreateSQLQuery command
i want to return the customer list using IList<Customer>;
i convert the IList into IList<T> object.
but its get following error

"Unable to cast object of type 'System.Object[]' to type Customer"


Code like this :

public IList<Customer> Execute()
{
string sqlString="Select * from Customer";
IList list = _session.CreateSQLQuery(sqlString).List();
IList<Customer> CustomerList= ConvertToListOf<Customer>(list);
retun CustomerList;
}

public static IList<T> ConvertToListOf<T>(IList iList)
{
IList<T> result = new List<T>();
foreach (T value in iList)
result.Add(value);

return result;
}

James Crowley

unread,
Mar 16, 2010, 4:53:21 AM3/16/10
to nhu...@googlegroups.com
Hi there,

You really don't need that ConvertToListOf method. Why not just use the generic version of "List" :

IList<Customer> customerList = _session.CreateSQLQuery(sqlString).List<Customer>();

If there's something weird going on with your mappings the new exception might give you a bit more info too...

James




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


Oskar Berggren

unread,
Mar 16, 2010, 4:53:04 AM3/16/10
to nhu...@googlegroups.com
1. You haven't told NHibernate to create Customer objects. SQL can be
used to return anything. See
http://nhforge.org/doc/nh/en/index.html#d0e9908
2. I suppose that "Select * from Customer" isn't the actual SQL
statement? If it is, use HQL or the Criteria API instead.

/Oskar


2010/3/16 waruni <war...@gmail.com>:

waruni

unread,
Mar 16, 2010, 4:58:22 AM3/16/10
to nhusers
Hi

I followed the above query but i gor following error

"The value "System.Object[]" is not of type "Customer" and cannot be
used in this generic collection.
Parameter name: value"


waruni

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>


> > .
> > For more options, visit this group at

> >http://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -

James Crowley

unread,
Mar 16, 2010, 5:03:25 AM3/16/10
to nhu...@googlegroups.com
yeah - see Oskar's response and the link he sent... I was entirely blind to the fact you were executing SQL instead of HQL. NHibernate won't automatically hydrate your mapped entities if you're using arbitrary sql, unless you tell it what entities to expect.

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Diego Mijelshon

unread,
Mar 16, 2010, 1:38:56 PM3/16/10
to nhusers
return _session.CreateSQLQuery("select * from Customer")
                      .AddEntity(typeof(Customer))
                      .List<Customer>();

You don't need to manually convert the values, but you DO need to specify the entities you're retrieving when using SQL Queries.


   Diego


Reply all
Reply to author
Forward
0 new messages