Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>();
like image 290
maxs87 Avatar asked Jan 15 '23 23:01

maxs87


1 Answers

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>();
like image 197
Firo Avatar answered Jan 28 '23 14:01

Firo