LINQ - Cast error - Asked By Donnie Talley on 25-Oct-11 08:06 PM

 /// <summary>
        /// Checks in the item with the given ISBN & copy number.
        /// </summary>
        /// <param name="isbn">The ISBN of the item to be checked in.</param>
        /// <param name="copyNo">The item's copy number.</param>
        public void CheckIn(int isbn, short copyNo)
{
            Require.IsGreaterThan<int>(isbn, 0, "isbn");
            Require.IsGreaterThan<int>(( int ) copyNo, 0, "copyNo");


            try
            {
                using ( LibraryDataContext dataContext = new LibraryDataContext() )
                {
                    var CheckQuery  = from CHQ in dataContext.loans
                                     where CHQ.isbn == isbn && CHQ.copy_no == copyNo
                                      select new 
                                     {
                                         CHQ.isbn,
                                         CHQ.copy_no,
                                         CHQ.title_no,
                                         CHQ.member_no,
                                         CHQ.due_date,
                                         CHQ.out_date,
                                     };


                     var  entity1 = CheckQuery.SingleOrDefault();


                    if ( entity1 == null )
                    {
                        throw new LibraryException(ErrorCode.ItemNotFound);
                    }


                    loanhist entity2 = new loanhist()
                    {
                        isbn = entity1.isbn,
                        copy = entity1.copy_no,
                        member_no = entity1.member_no,
                        due_date = new DateTime?(entity1.due_date),
                        in_date = new DateTime?(DateTime.Now),
                        out_date = entity1.out_date,
                        title_no = entity1.title_no
                    };


                    dataContext.loanhists.InsertOnSubmit(entity2);
                    entity1.copy.on_loan = 'N';
                    dataContext.loans.DeleteOnSubmit(entity1);
                    dataContext.SubmitChanges();
                }


                
            }
            catch (SqlException ex)
            {
                throw new LibraryException(ErrorCode.CheckInFailed, ex.Message);
            }


           

}
Adam replied to Donnie Talley on 26-Oct-11 03:49 PM
Where exactly is there error occurring?  I believe you can take out the new datetime? because it's basically redundant.  Try that and/or give more information on what and where the error is occurring.
Asked By Donnie Talley on 05-Dec-11 09:47 AM
member no, due date and checkout date  will have null value when the query returns from the database si I solve the problem by using the null operator ?? which will return a default or the value thaat returns from the database