有人可以帮帮我吗?我的家伙我需要一个Lambda表达式来处理这个sql语句。
SELECT DISTINCT TOP 1000 hotel.ID
,hotel.[LastModifiDate] as lmf
,hotel.[HotelID]
,hotel.[Description]
,hotel.[PercentOff] as poff
,hotel.[ActionState] as acs
,hotel.[LanguageID], x.avgPrice AS Price1
FROM tb_hotel as hotel
LEFT JOIN tb_room_hotel as roomhotel1
ON (hotel.HotelID = roomhotel1.HotelID)
LEFT JOIN
(
SELECT AVG(roomhotel2.Price) as avgPrice, roomhotel2.HotelID
FROM tb_room_hotel1 as roomhotel2
GROUP BY roomhotel2.HotelID
) x
ON (hotel.HotelID = x.HotelID)
WHERE hotel.LanguageID=1 ORDER BY avgPrice DESC
GO
我写了这段代码,但代码不正确......
var hotels1 = tb_hotel .GroupJoin(tb_room_hotel , chid => chid.HotelID,
crid => crid.HotelID, (chid, crid) => new { chid, crid })
.Where(rec => rec.chid.tb_Language.Title == lang && rec.chid.ActionState != 0)
.SelectMany(@t => @t.crid.DefaultIfEmpty(),(@t, c) => new { @t, c })
.OrderBy(@t => @t.c.Price)
.Where(@t => @t.c.Price > 0).Select(@t => @t.c.Price).Average();
table 1 = Hotels
teble 2 = Room of hotels
////////////////////////
I want show Price one night in Hotel. Sort by ASC & DESC with Lambda expression.
答案 0 :(得分:2)
我认为这段代码是正确的。
var hotels1 = tb_hotel.Join(tb_room_hotel, chid => chid.HotelID, crid => crid.HotelID, (chid, crid) => new { chid, crid })
.Where(x => x.crid.tb_Language.Title == lang && x.chid.ActionState != 0)
.GroupBy(t => new { HID = t.chid.HotelID })
.Select(g => new { Average = g.Average(p => p.crid.Price), HID = g.Key.HID }).OrderBy(c => c.Average).ToList();