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

Categories

0 votes
8.4k views
in Technique[技术] by (71.8m points)

Asp.net core: count hotel booking date

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...