2012-07-18 101 views
1

我有這樣的SQL查詢:SQL查詢加入NHibernate的與QueryOver

select pr.*, det.Description, det.Name 
from Product pr 
inner join ProductDetail det on det.Product_id = pr.id 
where pr.Id = XX and det.IsDefault = yy 

我怎樣才能做到這一點與QueryOver?

感謝,

更新: 「公關*」

public ProductMap() 
{ 
    Id(x => x.Id).GeneratedBy.Native(); 
    Map(x => x.Code) 
     .Length(20) 
     .Not.Nullable(); 
    Map(x => x.CreationDate).Not.Nullable(); 
    Map(x => x.IsDeleted); 
    Map(x => x.Price); 
    HasManyToMany(x => x.Categories) 
     .AsSet() 
     .Cascade 
     .SaveUpdate() 
     .Table("ProductsCategories"); 
} 

public class ProductDetailMap : ClassMap<ProductDetail> 
{ 
    public ProductDetailMap() 
    { 
     Id(x => x.Id).GeneratedBy.Native(); 
     Map(x => x.Name) 
      .Length(50) 
      .Not.Nullable(); 
     Map(x => x.Description) 
      .Length(250); 
     Map(x => x.IsDefault); 
     References(x => x.Language); 
     References(x => x.Product); 
    } 
} 
+0

什麼類型你想出去嗎? – 2012-07-18 22:32:09

+0

@AndrewWhitaker匿名類型。 – 2012-07-19 03:42:08

回答

2

據我所知QueryOver沒有等價的一部分,這將是很好的。 這意味着你將不得不在你的查詢中手動指定pr的每個屬性。

查看文檔here

節「預測」但它會是這樣的:

Product productAlias = null; 
ProductDetail productDetail = null; 
var query = session.QueryOver(() => productAlias) 
        .Inner.JoinAlias(() => productAlias.ProductDetails,() => productDetail) 
        .Where(() => productAlias.Id == XX && productDetail.IsDefault == YY) 
        .SelectList(list => list 
         .Select(() => productAlias.Id) 
         .Select(() => productAlias.Property1) 
         .Select(() => productAlias.Property2) 
         // and so on... 
         .Select(() => productDetail.Description) 
         .Select(() => productDetail.Name) 
        ); 

// One way of doing it... 
// Will give you a list of object arrays matching the result 
var results1 = query.List<object[]>(); 

// Another way... 
// You need to define a class that has all the properties your are querying for 
// If we create a class like that called "MySummaryClass" you can do: 
var results2 = query.TransformUsing(Transformers.AliasToBean<MySummaryClass>()).List<MySummaryClass>(); 
+0

嗯,做兩個查詢比較容易,然後在合併後使用linq,這是工作正常 – 2012-07-19 16:27:39