-3

こんにちは、私はこのような 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 であるため、コンパイラはこれを実行できないというエラーをスローしています。

4

1 に答える 1

1

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();       
于 2013-07-04T04:25:43.263 に答える