LINQ - Thenull value cannot be assigned to a member with type System.DateTime

Asked By Donnie Talley on 12-Nov-11 03:57 PM
  list = (from item1 in dataContext.copies
                                     where item1.isbn == isbn && item1.copy_no == copyNo


                                     select new Item
                                    {
                                        Isbn = item1.isbn,
                                       CopyNumber = item1.copy_no,
                                       Loanable = item1.item.loanable.HasValue,
                                       Cover = item1.item.cover,
                                       Synopsis = item1.title.synopsis,
                                       Title = item1.title.ToString(),
                                       Author = item1.title.author,
                                       Translation = item1.item.translation,
                                       MemberId = item1.loan.member_no,
                                       DueDate = item1.loan.due_date,
                                       CheckoutDate = item1.loan.out_date,
                                    }).SingleOrDefault<Item>();
Riley K replied to Donnie Talley on 12-Nov-11 07:51 PM



You datetime column is returning null value, and in your property you have not defined nullable datetime to avoid this error 

Here in your code DueDate = item1.loan.due_date, is returning null

so define it like this DateTime? DueDate


Regards

Kirtan Patel replied to Donnie Talley on 12-Nov-11 10:53 PM
if you want to assign null value to DateTime type you have to tell c# to consider as NULLABLE .A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true or false, or null. The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

DateTime? value = null;

Suchit shah replied to Donnie Talley on 13-Nov-11 01:16 AM
using a cast to DateTime? and null coalescing operator:
var test = _dataContext.GetTable<Car>
                     .Select(c => 
                         c.MechanicVisits
                          .Select(m => m.ServiceRecord)
                          .Select(s => (DateTime?)s.ServiceDate)
                          .OrderByDescending(d => d)
                          .FirstOrDefault() ?? new DateTime(1900, 1, 1)
                      ).ToList();

Note the use of the paramatered constructor for the "default" date - DateTime.MinValue can't be used here as it throws an out of range exception when converted to SQL, andSqlDateTime.MinValue can't be used as it is non-nullable (so the coalesce operator becomes invalid).

So do the change on you LINQ query as i have given in example for the Date time Varable and it get solved your problem
Sunil Darji replied to Donnie Talley on 19-Nov-11 04:26 AM

    here casting problem..
  
    DueDate = item1.loan.due_date,       // here item1.loan.due_date returning null
    CheckoutDate = item1.loan.out_date,    //here item1.loan.out_date returning null

    so cast datetime field like

      DueDate = (DateTime?) item1.loan.due_date,
   CheckoutDate = (DateTime?)item1.loan.out_date,   

  use like this

 Happy coding


 
Sunil Darji replied to Donnie Talley on 19-Nov-11 04:28 AM
 
  try this one and let me know

list = (from item1 in dataContext.copies
                                     where item1.isbn == isbn && item1.copy_no == copyNo


                                     select new Item
                                    {
                                        Isbn = item1.isbn,
                                       CopyNumber = item1.copy_no,
                                       Loanable = item1.item.loanable.HasValue,
                                       Cover = item1.item.cover,
                                       Synopsis = item1.title.synopsis,
                                       Title = item1.title.ToString(),
                                       Author = item1.title.author,
                                       Translation = item1.item.translation,
                                       MemberId = item1.loan.member_no,
                                       DueDate = (DateTime?) item1.loan.due_date,
                                       CheckoutDate = (DateTime?)item1.loan.out_date,
                                    }).SingleOrDefault<Item>();