C# 查找DateTime和DateTime之间的差异
嗨,我写了一个像这样的linq查询C# 查找DateTime和DateTime之间的差异,c#,linq,datetime,C#,Linq,Datetime,嗨,我写了一个像这样的linq查询 IssuedBooks = (from transaction in db.BookTransaction join tag in db.BookTagMaster on transaction.BookTagID equals tag.ID where tag.IsTagActive == true join book in db.BookMaster on tag.BookID equals book.ID join aut
IssuedBooks = (from transaction in db.BookTransaction
join tag in db.BookTagMaster on transaction.BookTagID equals tag.ID
where tag.IsTagActive == true
join book in db.BookMaster on tag.BookID equals book.ID
join author in db.AuthorMaster on book.AuthorID equals author.ID
join category in db.CategoryMaster on book.CategoryID equals category.ID
join publisher in db.PublisherMaster on book.PublisherID equals publisher.ID
select new BookIssuedView
{
ID = transaction.ID,
EmployeeName = transaction.EmployeeName,
IssuedDate = transaction.IssuedDate,
ReturnDate = transaction.ReturnDate,
BookName = book.Name,
AuthorName = author.Name,
CategoryName = category.Name,
PublisherName = publisher.Name,
SiteID = tag.SiteID,
BuildingID = tag.BuildingID,
LateFees = transaction.LateFees,
DueDate = transaction.DueDate,
LateBy = (!transaction.IsReturned)?0:(transaction.ReturnDate - transaction.DueDate).TotalDays
}).ToList();
但是我的ReturnDate是一个类型为DateTime的可空变量?DueDate只是DateTime,因此编译器抛出了一个错误,即无法执行此操作。有人能帮我解决此问题吗?您可以检查可为空的日期时间的HasValue属性,然后将DateTime的Value属性与DueDate进行比较:
IssuedBooks = (from transaction in db.BookTransaction
join tag in db.BookTagMaster on transaction.BookTagID equals tag.ID
where tag.IsTagActive == true
join book in db.BookMaster on tag.BookID equals book.ID
join author in db.AuthorMaster on book.AuthorID equals author.ID
join category in db.CategoryMaster on book.CategoryID equals category.ID
join publisher in db.PublisherMaster on book.PublisherID equals publisher.ID
select new BookIssuedView
{
ID = transaction.ID,
EmployeeName = transaction.EmployeeName,
IssuedDate = transaction.IssuedDate,
ReturnDate = transaction.ReturnDate,
BookName = book.Name,
AuthorName = author.Name,
CategoryName = category.Name,
PublisherName = publisher.Name,
SiteID = tag.SiteID,
BuildingID = tag.BuildingID,
LateFees = transaction.LateFees,
DueDate = transaction.DueDate,
LateBy = (!transaction.IsReturned && !transaction.ReturnDate.HasValue)?0:(transaction.ReturnDate.Value - transaction.DueDate).TotalDays
}).ToList();
我只需要一个建议,告诉我如何找出日期和时间之间的天数差异?DateTime请帮忙哦我修好了我要做的就是这么晚了!transaction.IsReturned?0:transaction.ReturnDate.Value-transaction.DueDate.TotalDays当ReturnDate为空时,您希望发生什么情况?@erNodeWeard我下面的回答正好满足了这一点,假设OP希望在这种情况下得到与IsReturned=false相同的结果。也许我不应该做出这样的假设: