如果有人可以帮我将以下SQL转换为nhibernate linq到QueryOver API,我将非常感激。
SELECT p.ManufacturerName as Name, sum(ps.QtyAvail) as QuantityAvailable
from Product p
inner join (select ProductId, QtyAvail
from ProductStats ps
where ps.QtyAvail > 0) ps on p.ProductId = ps.ProductId
where ltrim(rtrim(isnull(p.ManufacturerName, ''))) <> ''
group by p.ManufacturerName
order by Name
到目前为止,这是我编写和运行的唯一内容。
var o = Session
.Query<Product>()
.Where(p => p.ManufacturerName != null && p.ManufacturerName.Trim() != string.Empty)
.Join(Session.Query<ProductStats>().Where(ps => ps.QtyAvail > 0), product => product.ProductId, stats => stats.ProductStatId,
(product, stats) => new { Name = product.ManufacturerName, QuantityAvailable = stats.QtyAvail })
.GroupBy(q => q.Name)
.Select(g => new { Name = g.Key, QuantityAvailable = g.Sum(v => v.QuantityAvailable) });
提前致谢。
答案 0 :(得分:2)
确定,
在安德鲁·惠特克http://blog.andrewawhitaker.com/queryover-series/
中找到了很多信用的答案 [TestMethod]
public void CanGetManufacturersWithOnHandQuantities()
{
ProductStats statsAlias = null;
ManufacturersInStock manufacturersInStock = null;
var o = Session
.QueryOver<Product>()
.OrderBy(p => p.ManufacturerName).Asc
.WhereStringIsNotNullOrEmpty(p => p.ManufacturerName)
.JoinQueryOver(p => p.Stats, () => statsAlias)
.Where(ps => ps.QtyAvail > 0)
.SelectList(l => l.SelectGroup(p => p.ManufacturerName).WithAlias(() => manufacturersInStock.Name)
.SelectSum(() => statsAlias.QtyAvail).WithAlias(() => manufacturersInStock.InStockCount))
.TransformUsing(Transformers.AliasToBean<ManufacturersInStock>());
var results = o.List<ManufacturersInStock>();
Assert.IsTrue(o.RowCount() > 0);
}
public class ManufacturersInStock
{
public string Name { get; set; }
public int InStockCount { get; set; }
}
public static class QueriesExtentions
{
public static IQueryOver<E, F> WhereStringIsNotNullOrEmpty<E, F>(this IQueryOver<E, F> query, Expression<Func<E, object>> propExpression)
{
var prop = Projections.Property(propExpression);
var criteria = Restrictions.Or(Restrictions.IsNull(prop), Restrictions.Eq(Projections.SqlFunction("trim", NHibernateUtil.String, prop), ""));
return query.Where(Restrictions.Not(criteria));
}
}