我在網上進行了研究,大多數結果都是從sql轉換到linq,很少有linq到sql。將Linq轉換爲SQL
這是我要轉換爲SQL代碼:
using (CommerceEntities db = new CommerceEntities())
{
try
{
var query = (from ProductOrders in db.OrderDetails
join SelectedProducts in db.Products on ProductOrders.ProductID
equals SelectedProducts.ProductID
group ProductOrders by new
{
ProductId = SelectedProducts.ProductID,
ModelName = SelectedProducts.ModelName
} into grp
select new
{
ModelName = grp.Key.ModelName,
ProductId = grp.Key.ProductId,
Quantity = grp.Sum(o => o.Quantity)
} into orderdgrp where orderdgrp.Quantity > 0
orderby orderdgrp.Quantity descending select orderdgrp).Take(5);
RepeaterItemsList.DataSource = query;
RepeaterItemsList.DataBind();
}
catch (Exception exp)
{
throw new Exception("ERROR: Unable to Load Popular Items - " +
exp.Message.ToString(), exp);
}
}
您可以隨時使用Profiler來看看實際的查詢生產,或插入某種形式的日誌記錄。 – 2012-07-30 06:53:43
嗨,我在Microsoft Visual Studio中使用SQL Server。 – user1529419 2012-07-30 06:56:42
你爲什麼不嘗試Linqpad,它會幫助你理解事物,你也可以看到生成的SQL。 – 2012-07-30 06:58:23