Reputation: 39
Is there any way to count the day of booking? either using lambda or sqlite query thanks, can find the solution for a while thanks~
The bookingInput is from a webpage of booking which contains rooms, checkin and check out.
The below code is Create.cshtml.cs in Booking folder (created by scaffolding)
int count = bookingInput.CheckOut.DayOfWeek - booking.CheckIn.DayOfWeek;
booking.Cost = count * theRoom.Price;
Upvotes: 0
Views: 160
Reputation: 21421
You could use the TimeSpan.TotalDays Property to get the number of days between two dates.
Check the following code:
//get data from the Repository.
var result = _repo.GetBookings().Select(c => new
{
Id = c.Id,
Name = c.Name,
RoomNumner = c.RoomNumber,
CheckIn = c.CheckIn,
CheckOut = c.CheckOut,
Days = (c.CheckOut - c.CheckIn).TotalDays,
RoundDays = Math.Round((c.CheckOut - c.CheckIn).TotalDays)
});
The result as below:
The test data in the Repository:
public interface IDataRepository
{
List<BookingViewModel> GetBookings();
}
public class DataRepository : IDataRepository
{
public List<BookingViewModel> GetBookings()
{
return new List<BookingViewModel>()
{
new BookingViewModel(){ Id=101, Name="David", RoomNumber=1001, CheckIn= new DateTime(2021,1,18,15,30,0), CheckOut=DateTime.Now },
new BookingViewModel(){ Id=102, Name="Jack", RoomNumber=2001, CheckIn= new DateTime(2021,1,15,10,30,0), CheckOut=DateTime.Now },
new BookingViewModel(){ Id=103, Name="Tom", RoomNumber=3001, CheckIn= new DateTime(2021,1,20,18,30,0), CheckOut=DateTime.Now },
};
}
}
Upvotes: 1