'slow performance of outlook addin to get contact item for distribution list

I have the following code it takes lot of time to retrieve contact items from the distribution list, exchange server. Is there anything can be done to tune it s performance

public Outlook.AddressEntry GetDistributionListMembers(string sParamName,Outlook.Application _OutlookApp)
    {
        try
        {
            List<string> allAddressEntriesList = new List<string>();
            Outlook.AddressLists addrLists = _OutlookApp.ActiveExplorer().Session.AddressLists;
            var receipientContactList = new List<Outlook.AddressEntry>();
            foreach (Outlook.AddressList addrList in addrLists)
            {
                if (addrList.AddressEntries.Count <= 0) continue;
                receipientContactList.AddRange(addrList.AddressEntries.Cast<Outlook.AddressEntry>()
                    .Where(x => x.Address.ToLower().Equals(sParamName.ToLower())));

if debug

                 Debug.print(addrList.Name);

endif

                if (receipientContactList.Count > 0) break;
            }
            return receipientContactList.FirstOrDefault(x => x.GetContact().HasPicture) ??
                   receipientContactList.FirstOrDefault();

        }
        catch (System.Exception ex)
        {
            WriteLog(System.Reflection.MethodBase.GetCurrentMethod().Name,

ex.Message); return null; }

    }


Solution 1:[1]

Firstly, never loop through all entries in the address book. You are matching on an address; why not use Namespace.CreateRecipient / Recipient.Resolve?

Secondly, you are assuming that all address entries come from your Contacts folder. In case of GAL under Exchange, this is not the case, and AddressEntry.GetContact() will return null, resulting in an exception.

And if your entries always come from the Contacts folder, why not use Namespace.GetDefaultFolder(olFolderContacts) and then use MAPIFolder.Items.Find?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Streblechenko