1

我有这个 SQL 查询:

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);
    }
}
4

1 回答 1

2

据我所知,QueryOver 没有相当于“pr.*”的部分,这很好。这意味着您必须在查询中手动指定 pr 的每个属性。

请参阅此处文档中的“预测”部分

但它会是这样的:

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>();
于 2012-07-19T09:13:03.273 回答