这个linq为我提供了所有房间作为按rateCode分组的列表。
var results = (from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
group r.RoomTypesPerCenter.RoomTypes.Code by r.CRS_RateTypes.Name into g
select new { rateCode = g.Key, roomName = g.ToList() });
但是现在我必须通过数据库中的Integer对结果进行排序,命名为Order:
var results = (from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
orderby r.Order ascending
group r.RoomTypesPerCenter.RoomTypes.Code by r.CRS_RateTypes.Name into g
select new { rateCode = g.Key, roomName = g.ToList() });
这只订购房间的名称,而不是两者。
数据:
Order Rates RoomType
5 PER DBL
30 PER IND
15 BAR IND
10 BAR DBL
20 BAR URB
它应该给出这个结果,因为第一个是5和30(PER)然后是10,15和20(BAR):
{rateCode = PER, roomName = {DBL, IND} }
{rateCode = BAR, roomName = {DBL, IND, URB} }
但它让我回答:
{rateCode = BAR, roomName = {DBL, IND, URB} }
{rateCode = PER, roomName = {DBL, IND} }
感谢您的任何建议。
答案 0 :(得分:1)
数据库GROUP BY
查询结果的键顺序未定义。
您需要在分组后应用排序,例如
var results =
(from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
group r by r.CRS_RateTypes.Name into g
orderby g.Min(r => r.Order)
select new
{
rateCode = g.Key,
roomName = (from r in g orderby r.Order select r.RoomTypesPerCenter.RoomTypes.Code).ToList()
});