This is my constructor to transform an Outlook contact into a Dynamics CRM contact:
public Contact(ContactItem contactItem)
{
LogicalName = EntityLogicalName;
FirstName = contactItem.FirstName;
LastName = contactItem.LastName;
//if no last name, use the first name
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.FirstName;
//if no First name, use the Full Name.
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.FullName;
//if no last name or first name, use the company name
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.CompanyName;
//If no last name, no first name and no company name, use the email address.
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.Email1Address;
//if no Last name, no first name, no company name, no email address, default to naam niet bekend.
if (String.IsNullOrEmpty(LastName))
{
LastName = @"Naam niet bekend";
}
}
}
}
else
{
FirstName = "";
}
}
Address1_City = contactItem.BusinessAddressCity;
Address1_Country = contactItem.BusinessAddressCountry;
Address1_Line1 = contactItem.BusinessAddressStreet;
Address1_PostalCode = contactItem.BusinessAddressPostalCode;
Telephone1 = contactItem.BusinessTelephoneNumber;
Telephone2 = contactItem.HomeTelephoneNumber;
Fax = contactItem.BusinessFaxNumber;
Address2_Line1 = contactItem.HomeAddressStreet;
Address2_City = contactItem.HomeAddressCity;
Address2_PostalCode = contactItem.HomeAddressPostalCode;
Address2_Country = contactItem.HomeAddressCountry;
EMailAddress1 = contactItem.Email1Address;
EMailAddress2 = contactItem.Email2Address;
EMailAddress3 = contactItem.Email3Address;
}
As you can see, I got 5 nested if
s to determine what I should use for LastName
. I don't like how it looks, but the only other option I can see is putting them in sequence instead of nested. However, that would mean that instead of usually having only 1-3 ifs, it would be forced into doing 5 ifs every single time. during tests with 1800 records, it already felt noticeably slower if it got to the literal string.
Is there some really easy way (or some really obscure way) of doing this which I missed?