1
        var query =
            (from Contact con in e.Results
             from ContactPhoneNumber phn in con.PhoneNumbers
             from ContactEmailAddress email in con.EmailAddresses.DefaultIfEmpty()
             where con.DisplayName.Contains(txtContasctSearch.Text)
             select new person()
             {
                 displayName = con.DisplayName,
                 displayEmail = (email.EmailAddress == null ? String.Empty : email.EmailAddress),
                 displayPhone = phn.PhoneNumber
             }).ToList();

The EmailAddress field is not always available. However, I would still like to bring it back if it exists. I want to mimic a Left Join however, the code above returns an error.

Any ideas?

The error I'm receiving is:

 System.NullReferenceException occurred
   _HResult=-2147467261
    _message=NullReferenceException
     HResult=-2147467261
    Message=NullReferenceException
    Source=wpChoreList
    StackTrace:
       at wpChoreList.personSetup.<Contacts_SearchCompleted>b__8(<>f__AnonymousType1`2    h__TransparentIdentifier1)
   InnerException: 
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
webdad3
  • 8,893
  • 30
  • 121
  • 223

1 Answers1

2

You are checking wrong value for null:

displayEmail = (email.EmailAddress == null ? String.Empty : email.EmailAddress),

email should be empty, not email.EmailAddress, try changing that line to this one:

displayEmail = (email == null ? String.Empty : email.EmailAddress),
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • That was it! Dang something so simple... Thank you. I will award as answer as soon as it lets me. – webdad3 Feb 10 '13 at 14:41