Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
question from:https://stackoverflow.com/questions/65869790/asp-net-core-count-hotel-booking-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
8.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

enter image description here

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 },
       };
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...