[英]Sql Query to Linq To Sql
對於Linq to Sql中的多表查詢,我遇到一些困難。 我有3張桌子:
我想檢索產品的最小值(價格)。 我能夠獲得正確的Sql查詢,但是現在我需要將該查詢“翻譯”為Linq To Sql。 你能幫我么 ?
這里是Sql查詢:
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;
這是一個解決方案:
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();
此處的nullable很重要,否則,如果系統中沒有產品,行或價格,它將失敗。
它與翻譯查詢無關 ,而與利用linq2sql提供的功能有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.