-3

Hi i have written a linq query like this

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();         

but my ReturnDate is a nullable variable that is of type DateTime? and the DueDate is just DateTime hence the compiler is throwing an error that this cann not be done can any body help me work arround this

mishik
  • 9,973
  • 9
  • 45
  • 67
Vishweshwar Kapse
  • 921
  • 6
  • 23
  • 43
  • i just need as suggestion as to how i can find the Difference in days between DateTime? and DateTime pls help – Vishweshwar Kapse Jul 04 '13 at 04:22
  • Oh i fixed it all i had to do was this `LateBy = (!transaction.IsReturned)?0:(transaction.ReturnDate.Value - transaction.DueDate).TotalDays ` – Vishweshwar Kapse Jul 04 '13 at 04:25
  • 1
    What do you want to happen when the ReturnDate is null? – Emond Jul 04 '13 at 04:25
  • @ErnodeWeerd My answer below caters for that, assuming the OP wants the same result in that case as when IsReturned = false. Perhaps I shouldn't have made that assumption :) – Squid Jul 04 '13 at 04:28

1 Answers1

1

You could check the HasValue property of the nullable date time, and then compare the Value property (in this case a DateTime) with the 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();       
Squid
  • 4,560
  • 1
  • 12
  • 8