c# - Nhibernate throws Object of type 'System.Linq.EnumerableQuery`1[Entity]' cannot be converted to type 'System.Linq.IQueryable`1[System.Object[]]' -


using nhibernate orm
trying 1 entity

   var users = getusersdata(repository);    return users.where(f => f.id == id).firstordefault();//there exception 

exception:

object of type 'system.linq.enumerablequery1[entity]' cannot converted type 'system.linq.iqueryable1[system.object[]]'.

but when way:

var users = getusersdata(repository); return users.where(f => f.id == id).toarray().firstordefault(); 

it runs fine. point me, what's wrong?

in getusersdata :

private static iqueryable<user> getusersdata(iuserrepository repository)         {             return repo.getall().select(user => new user                 {                     id = user.id,                     phones = user.phones.select(s => new phone() { number = s.number }),                     ...                 }).asqueryable();  

.asqueryable() wrap given iqueryable nh needlessly new queryable since nh default use object arrays results , transform later fail casting.

also selecting user user prevent changetracking , needless copying.

repo.getall().where(f => f.id == id).firstordefault(); should enough 

or better because of sessioncache usage

var user = session.get<user>(id); if (user == null)     // user given id not exist 

Comments