I have the following LINQ code:
docTypes = (from c in context.Citizenships join
cdt in context.Citizenship_Document_Types
on c.Country_Code equals cdt.Country_Code
from cd in context.Citizenship_Documents
.Where(cd => cd.Citizenship_Id == c.Citizenship_ID)
.DefaultIfEmpty()
where c.Citizenship_ID == citizenshipId
select new CitizenshipDocument
{
Id = (int?)cd.Citizenship_Document_Id??-1,
CitizenshipId = c.Citizenship_ID,
DocumentTypeId = cdt.Citizenship_Document_Type_Id,
DocumentTypeName = cdt.Citizenship_Document_Type_Name,
DocumentCode = cd.Citizenship_Document_Code.ToArray(),
ExpirationDate = cd.Expiration_Date,
IssueDate = cd.Issue_Date
}).ToList();
The issue is that when cd.Citizenship_Document_Code
returns null I get an error when using .ToArray()
. :
Object reference not set to an instance of an Object
How can I handle null values?