查找DateTime和DateTime之间的差异
本文关键字:DateTime 之间 查找 | 更新日期: 2023-09-27 18:21:09
嗨,我写了一个类似的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 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的可为null的变量?DueDate只是DateTime,因此编译器抛出了一个无法完成的错误。任何人都能帮助我处理这个吗?
您可以检查可为null的日期时间的HasValue属性,然后将Value属性(在本例中为DateTime)与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();