如何查看可用的酒店房间

时间:2018-11-25 08:04:41

标签: c# sql linq

我正在使用LINQ和SQL制作Windows形式的酒店预订系统。我有一些桌子,例如预订表和房间表。在预订表中,我有入住和退房日期。我没问题显示预订表中特定日期的那些房间,但是我不知道如何显示特定日期可用的房间。我不怎么比较或过滤预订表和房间表中的RoomsId

这是我如何在特定日期(入住和退房)从预订表获取房间的方法:

DateTime StartDateWantToBook = Convert.ToDateTime(dateTimePicker1.Value.ToString());
DateTime EndDateWantToBook = Convert.ToDateTime(dateTimePicker2.Value.ToString());

var ReservedRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId where StartDateWantToBook <= b.EndDate && b.StartDate <= EndDateWantToBook
 select new {
  RomId = b.RoomId,
   Floor = f.FloorName,
   RommsNr = u.RumNummer,
   Room_Type = ty.AmountRomms
   // But Here by somehow I think I have to run
   // another Linq query to filter RoomsId and show only those who do not exists in Reservation table.                             
 }
).ToList();
dataGridView1.DataSource = ReservedRooms;

因此,问题是如何显示“房间”表中所有在特定日期不存在于Reservartionroom中的房间。再次谢谢你!

3 个答案:

答案 0 :(得分:0)

您需要在特定日期选择AllRoomsReservedRooms,然后按列表保留条件过滤AllRoom。那是我的主意,也许包含是错误的。

DateTime StartDateWantToBook = Convert.ToDateTime(dateTimePicker1.Value.ToString());
DateTime EndDateWantToBook = Convert.ToDateTime(dateTimePicker2.Value.ToString());

var AllRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId select new {
 RomId = b.RoomId,
  Floor = f.FloorName,
  RommsNr = u.RumNummer,
  Room_Type = ty.AmountRomms
  // But Here by somehow I think I have to run
  // another Linq query to filter RoomsId and show only those who do not exists in Reservation table.                              
}).ToList();

var ReservedRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId where StartDateWantToBook <= b.EndDate && b.StartDate <= EndDateWantToBook select new {
 RomId = b.RoomId,
  Floor = f.FloorName,
  RommsNr = u.RumNummer,
  Room_Type = ty.AmountRomms
  // But Here by somehow I think I have to run
  // another Linq query to filter RoomsId and show only those who do not exists in Reservation table.

}).ToList();

var result = AllRoom.Except(ReservedRooms);
dataGridView1.DataSource = ReservedRooms;

答案 1 :(得分:0)

我认为您需要左外部联接AllRooms和ReservedRooms才能仅使用具有特定日期的where子句来获得房间

答案 2 :(得分:0)

这必须为您工作。

var AvailebleRooms = (from u in db.Room
                      where (!u.Reservation.Any(b => b.EndDate >= StartDateWantToBook && b.StartDate <= EndDateWantToBook))
                      select u).ToList();