0

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?

1
  • I recommend to split this up so it's not one big ball of code with different responsibilities. Maybe create a new constructor or static constructor method on CitizenshipDocument that takes just c and cd. Or if that isn't an option, a local Func that takes c and cd, and returns CitizenshipDocument. Commented Aug 31, 2010 at 3:20

2 Answers 2

1

You would handle nulls in a LINQ query the same as you would handle them anywhere else. Don't dereference a null value! For instance:

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 == null ?
                    null : 
                    cd.Citizenship_Document_Code.ToArray(),
                ExpirationDate = cd.Expiration_Date,
                IssueDate = cd.Issue_Date
            }).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I thought I had tried this and had it fail. I must have had some other syntax error.
0

Consider handling the null with an appropriate null-object using something like the following extension method:

public static T ToNonNull<T>(this T input) where T : class, new()
{
  if (input != null)
  {
    return input;
  }
  return new T();
}

Usage would be something like:

DocumentCode = cd.Citizenship_Document_Code.ToNonNull().ToArray()

You could also have a specific ToNonNull() extension just for that type if you don't want a generic one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.