I have some difficulties when it comes to multi-table query in Linq to Sql. I have 3 tables:
I'd like to retrieve the min(Price) for a product. I was able to get the right Sql query, but now I need to 'translate' this query into Linq To Sql. Can you please help me ?
Here the Sql query:
SELECT Min(cp.Price)
FROM Products p, Rows r, ConstantPrices cp
WHERE p.ProductId = r.ProductId AND
r.RowId = cp.RowId AND
p.ProductId = XXX;
Here's a solution:
decimal? min = (from p in db.Products
join r in db.Rows on p.ProductId equals r.ProductId
join cp in db.ConstantPrices on r.RowId equals cp.RowId
where p.ProductId == 1
select cp.Price).Min();
var productQuery = from p in myContext
where p.id == someId
select (double?) p.Row.Price;
var minPrice = productQuery.Min();
Its important the nullable in there, otherwise it would fail if there are no products or rows or prices in the system.
Its not about translating queries, its about taking advantage of what linq2sql gives you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.