English 中文(简体)
Sql Query to Linq To Sql
原标题:Sql Query to Linq To Sql
  • 时间:2009-09-22 17:03:52
  •  标签:

I have some difficulties when it comes to multi-table query in Linq to Sql. I have 3 tables:

  • Product
  • Row (with Fk-ProductId)
  • Price (with Fk-RowId)

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 ?

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;
最佳回答

这里的解决办法是:

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();

否则,如果系统中没有产品或增长或价格,它就会失败。

www.un.org/Depts/DGACM/index_spanish.htm 它并不涉及翻译查询,而是关于利用Llinq2sql给你的询问。





相关问题
热门标签