大家好,我想获得数据库中提供的所有房间。下面是表格结构:
表室:
CREATE TABLE [dbo].[Room] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC),
);
餐桌预订:
CREATE TABLE [dbo].[Booking] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[RoomID] INT NOT NULL,
[DateFrom] DATETIME NOT NULL,
[DateTo] DATETIME NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Booking_Room] FOREIGN KEY ([RoomID]) REFERENCES [dbo].[Room] ([ID]),
);
这就是我到目前为止所做的事情,这是不对的。
var a = _RoomService.GetRooms()
.Where(a => a.ID <> (_BookingService.GetBookings()
.Where((b => b.DateFrom > thisDate.DateFrom ||
b.DateFrom < thisDate.DateFrom &&
b.DateTo > thisDate.DateTo ||
b.DateTo < thisDate.DateTo))
.ToList());
我希望获得用户选择“从日期和到期日期”时可用的所有房间。谢谢你的期待。
修改
这是我取得的成绩,但仍然没有结果......
var roomsBooked = from b in _BookingService.GetBookings()
where (b.DateFrom <= thisDate.DateFrom) && (b.DateTo >= thisDate.DateFrom) ||
(b.DateFrom <= thisDate.DateTo) && (b.DateTo <= thisDate.DateTo) ||
(b.DateFrom >= thisDate.DateFrom) && (b.DateTo >= thisDate.DateTo)
select b;
var availableRooms = _RoomService.GetRooms().Where(a => roomsBooked.Any(b => b.RoomID != a.ID));
欢迎任何可以提供帮助的人。我从昨天开始一直在努力:(谢谢
答案 0 :(得分:1)
var bookings = _BookingService.GetBookings()
.Where((b => b.DateFrom > thisDate.DateFrom ||
b.DateFrom < thisDate.DateFrom
&& b.DateTo > thisDate.DateTo
|| b.DateTo < thisDate.DateTo);
var a = _RoomService.GetRooms().Where(a => !bookings.Any(b=>b.RoomID == a.ID)).ToList());
答案 1 :(得分:1)
以下是我为您准备的示例。它与@ AD.Net asnwer完全相同,除了我的是用LINQ编写的。我把它放在一个控制台应用程序中,如果感兴趣的话,你可以测试和玩,只需复制和粘贴。
以下是代码:
class Program
{
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
static void Main(string[] args)
{
Program p = new Program();
}
public Program()
{
var _BookingService = new List<Booking>()
{
new Booking() { ID = 100, RoomID = 1, DateFrom = new DateTime(2016, 5, 15), DateTo = new DateTime(2016, 5, 17)},
new Booking() { ID = 200, RoomID = 2, DateFrom = new DateTime(2016, 5, 20), DateTo = new DateTime(2016, 5, 25) }
};
this.DateFrom = new DateTime(2016, 5, 14);
this.DateTo = new DateTime(2016, 5, 27);
var roomsBooked = from b in _BookingService
where
((this.DateFrom >= b.DateFrom) && (this.DateFrom <= b.DateTo)) ||
((this.DateTo >= b.DateFrom) && (this.DateTo <= b.DateTo)) ||
((this.DateFrom <= b.DateFrom) && (this.DateTo >= b.DateFrom) && (this.DateTo <= b.DateTo)) ||
((this.DateFrom >= b.DateFrom) && (this.DateFrom <= b.DateTo) && (this.DateTo >= b.DateTo)) ||
((this.DateFrom <= b.DateFrom) && (this.DateTo >= b.DateTo))
select b;
var availableRooms = GetRooms().Where(r => !roomsBooked.Any(b => b.RoomID == r.ID));
foreach (var room in availableRooms)
Console.WriteLine($"{room.Title}");
Console.ReadKey();
}
public List<Room> GetRooms()
{
return new List<Room>()
{
new Room() { ID = 1, Title = "Room 1" },
new Room() { ID = 2, Title = "Room 3" }
};
}
}
public class Room
{
public int ID { get; set; }
public string Title { get; set; }
}
public class Booking
{
public int ID { get; set; }
public int RoomID { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
}
您感兴趣的多汁部分就是这段代码:
var roomsBooked = from b in _BookingService
where
((this.DateFrom >= b.DateFrom) && (this.DateFrom <= b.DateTo)) ||
((this.DateTo >= b.DateFrom) && (this.DateTo <= b.DateTo)) ||
((this.DateFrom <= b.DateFrom) && (this.DateTo >= b.DateFrom) && (this.DateTo <= b.DateTo)) ||
((this.DateFrom >= b.DateFrom) && (this.DateFrom <= b.DateTo) && (this.DateTo >= b.DateTo)) ||
((this.DateFrom <= b.DateFrom) && (this.DateTo >= b.DateTo))
select b;
var availableRooms = GetRooms().Where(r => !roomsBooked.Any(b => b.RoomID == r.ID));
上面的代码首先选择预订的所有房间,并将它们放在roomsBooked
集合中。之后,我调用了GetRooms()
函数,它将获得你的&#34;数据库&#34;中的所有房间。然后我执行where()
过滤掉当前不在GetRooms()
列表/集合中的roomsBooked
的所有会议室。我希望这有点道理