2012-07-30 68 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