public void get_cheapest_supplier_for_product() { var product = new ProductBuilder().Build(); var supplier1 = new SupplierBuilder().Build(); var supplier2 = new SupplierBuilder().Build(); var supplier3 = new SupplierBuilder().Build(); product.AddSource(supplier1, 50); product.AddSource(supplier2, 48); product.AddSource(supplier3, 52); Session.Save(product); Session.Flush(); var cheapestSupplier = Session.CreateQuery( "select ps.Supplier from ProductSource ps where ps.Product.Id = :productId order by ps.Cost asc") .SetParameter("productId", product.Id) .SetMaxResults(1) .UniqueResult <Supplier>(); // the following LINQ approach also works, but uses an uneccessary join on Supplier in the subselect! //var cheapestSupplierId = Session.Query<ProductSource>() // .Where(s => s.Product.Id == product.Id) // .OrderBy(s => s.Cost) // .Select(s => s.Supplier.Id) // .Take(1); //var cheapestSupplier = Session.Query<Supplier>() // .Where(s => s.Id == cheapestSupplierId.Single()) // .Single(); Assert.AreEqual(supplier2, cheapestSupplier); }
public void get_cheapest_supplier_for_product() { var product = new ProductBuilder().Build(); var supplier1 = new SupplierBuilder().Build(); var supplier2 = new SupplierBuilder().Build(); var supplier3 = new SupplierBuilder().Build(); product.AddSource(supplier1, 50); product.AddSource(supplier2, 48); product.AddSource(supplier3, 52); Session.Save(product); Session.Flush(); Supplier cheapestSupplier = null; Assert.AreEqual(supplier2, cheapestSupplier); }