1
我試圖在LINQ中編寫此選擇,但我沒有成功修復它很長時間。我也試過LINQ - join with Group By and get average,但它在我的代碼中不起作用。很顯然,我錯了。LINQ - 多組加入並獲得平均值
SQL:
SELECT name_type, AVG(t.price) as avgPrice FROM type tp
JOIN location l ON l.ID_type = tp.ID
JOIN event e ON e.ID_location = l.ID
JOIN ticket t ON t.ID_event = e.ID
GROUP BY tp.name_type
LINQ:
var q3 = from l in db.location
join tp in db.type on l.ID_type equals tp.ID
join e in db.event on l.ID equals u.ID_location
join t in db.ticket on e.ID equals t.ID_event
group tp by new {Type_name = tp.type_name} into grp
select new
{
Type_name = grp.Key.type_name,
avgPrice = grp.Average(x => x.ticket.price)
};
謝謝你洙多!它完美的作品:)現在我可以去嘗試更難的選擇... – Matwosk 2013-05-03 11:51:08