maxs87
maxs87

Reputation: 2284

Nhibernate query over

Im trying to select only some fields from my table with code like this

IList<Product> res = sess.QueryOver<Product>()            
             .Select(x =>x.name)           
             .List<Product>();

No errors with this code but in runtime i got this: "Unable to perform find[SQL: SQL not available]" the value "Prod1 is not of type SympleFlhLINQ.Product and and cannot be used on this generic collection".

And will be very nice if someone tell me how i can fetch only product name and referenced category name width something like this

 IList<Product> res = sess.QueryOver<Product>() 
                .Select(x =>x.name)
                .Select(x=>x.Cat.CategoryName)
                .List<Product>();

Upvotes: 4

Views: 4058

Answers (1)

Firo
Firo

Reputation: 30803

IList<string> names = sess.QueryOver<Product>()            
         .Select(x =>x.Name)
         .List<string>();

or

ProductDto product = null;
Category category = null;
IList<ProductDto> res = sess.QueryOver<Product>()
    .JoinAlias(x => x.Category, () => category)
    .SelectList(list => list
        .Select(x => x.Name).WithAlias(() => product.Name)
        .Select(() => category.Name).WithAlias(() => product.CategoryName))
    .TransformUsing(Transformers.AliasToBean<ProductDto>())
    .List<ProductDto>();

Upvotes: 9

Related Questions