How to create a List variable constant in ASP.NET
In this code, I am trying to check which numbers are available at the time the user entered. To check if a room is free, I change the name of the room to all parameters and do a collision check. The problem is that when I change the name of the room, it also updates it in my list of existing books. Is there a way to make sure existing records are not being changed?
var existingBookings = _context.Bookings.ToList();
var booking = await _context.Bookings.SingleOrDefaultAsync(m => m.BookingID == id);
List<Room> roomlist = new List<Room>();
roomlist = (from product in _context.Room
select product).ToList();
List<Room> availableRooms = new List<Room>();
foreach (var item in roomlist)
{
booking.RoomName = item.RoomName;
if (BusinessLogic.BookingChecker.DoesBookingClash(booking, existingBookings) == null)
{
availableRooms.Insert(0, new Room { RoomID = item.RoomID, RoomName = item.RoomName });
}
booking.RoomName = "UNDEF";
}
Thank you for your help!
source to share
The way I fixed this was to add the new bool field suggested in the file Booking.cs
. When creating a suggested booking, it will automatically set to true. In mine, DoesRoomBookingClash
I added an if statement to ignore the database entry if the clause was true. After all changes are made, it is suggested to set the value to false.
source to share
To do this, you need to create a new instance Booking
. You can call the constructor and copy the properties you want, or you can implement the method ICloneable.Clone
.
public class Booking : ICloneable
{
public Booking Clone()
{
return new Booking() { RoomID = this.RoomID, etc. };
}
object ICloneable.Clone()
{
return this.Clone();
}
}
Then you call it:
booking = booking.Clone();
// use the new booking instance.
source to share