2012-07-30 73 views
3

我试着从我的表像这样的代码NHibernate的查询过

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

与此代码,但在运行时我得到这个没有错误,只选择一些字段:“无法执行查找[SQL:SQL不可用]“值”Prod1不是类型SympleFlhLINQ.Product并且不能在此泛型集合上使用“。

而且会是非常好的,如果有人告诉我怎样可以获取唯一的产品名称和引用的类别名称宽度像这样

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

你需要一个' IList '或者你可以使用DTO吗? – 2012-07-30 22:52:16

+0

DTO你的意思是使用类型像公共类ProductCollection:列表 { } ?? – maxs87 2012-08-02 11:46:10

回答

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

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>(); 
+0

谢谢!!!它工作正常。 – maxs87 2012-08-02 12:36:41

+0

即时通讯新手在stackoverflow和我不知道我可以接受答案大声笑。 sry – maxs87 2012-08-02 13:56:17

+0

http://meta.stackexchange.com/q/5234 – Firo 2012-08-02 17:05:33