nave

nave

  • NA
  • 31
  • 0

The cast to value type 'Int32' failed because the materializ

Oct 17 2013 6:16 AM
I have this code:

var item = (from se in _db.Events
  where se.ID == id
  select new PseudoEvent()
  {
  ID = se.ID,
  categoryId = se.Categories.ID,
  }).FirstOrDefault();

The "Events" and "Categories" tables have a relationship:
"Events" table > Category_ID = "Categories" table > ID

When i dont select a category for the event the "Category_ID" in the "Events" table stays "NULL" and in my code above i get this error:

The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

How can i fix that so the event item will be shown even if the Category_ID column is null?

Please advise
Thanks

Answers (2)