如何将动态变量-forach转换为Parallel.foreach()

本文关键字:Parallel foreach 转换 -forach 动态 变量 | 更新日期: 2023-09-27 18:26:39

如何将此foreach转换为Parallel.ForEach()

我们正在使用dynamic变量来获取outlook联系人。

有什么帮助吗?

List<ServiceReference1.Addressbooks> list = new List<ServiceReference1.Addressbooks>();
// dynamic Contacts = outlook.GetNamespace("Mapi").GetDefaultFolder(10);
try
{
    dynamic Contacts = outlook.Session.GetDefaultFolder(10);
    foreach (var items in Contacts.items)
    {
        try
        {
            ServiceReference1.Addressbooks ad = new ServiceReference1.Addressbooks();
            ad.FirstName = (string)items.FirstName;
            ad.JobTitle = (string)items.JobTitle;
            ad.MobileTelephoneNumber = (string)items.MobileTelephoneNumber;
            ad.BusinessTelephoneNumber = (string)items.BusinessTelephoneNumber;
            ad.BusinessFaxNumber = (string)items.BusinessFaxNumber;
            ad.Email1Address = (string)items.Email1Address;
            ad.Body = items.Body.Length > 999 ? (items.Body).Substring(0, 999) : items.Body;
            ad.CompanyName = (string)items.CompanyName;
            ad.LastModificationTime = (DateTime)items.LastModificationTime;
            list.Add(ad);
        }
        catch (Exception exp)
        {
            throw exp;
        }
    }
}
catch
{
}
return list;

谨致问候,Prasanth.k

如何将动态变量-forach转换为Parallel.foreach()

好的,试试这个代码,让我知道你的问题

Parallel.ForEach(Contacts.items, items =>
            {
                try
                {
                    ServiceReference1.Addressbooks ad = new ServiceReference1.Addressbooks();
                    ad.FirstName = (string)items.FirstName;
                    ad.JobTitle = (string)items.JobTitle;
                    ad.MobileTelephoneNumber = (string)items.MobileTelephoneNumber;
                    ad.BusinessTelephoneNumber = (string)items.BusinessTelephoneNumber;
                    ad.BusinessFaxNumber = (string)items.BusinessFaxNumber;
                    ad.Email1Address = (string)items.Email1Address;
                    ad.Body = items.Body.Length > 999 ? (items.Body).Substring(0, 999) : items.Body;
                    ad.CompanyName = (string)items.CompanyName;
                    ad.LastModificationTime = (DateTime)items.LastModificationTime;
                    list.Add(ad);
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            });