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 },
};
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…