将字符串列表转换为ICollection的MailAddresses

时间:2017-10-16 23:44:42

标签: c#

ICollection<MailAddress> toCollection = to.Split(',');

此操作失败,因为字符串不会自动MailAddress ...

如何迭代字符串,将每个实例设为MailAddress,然后将其添加到新的ICollection

1 个答案:

答案 0 :(得分:2)

你可以做到

using System.Linq;
....
IEnumerable<MailAddress> addresses = to.Split(',').Select(i => new MailAddress(i));

编辑:将其设为ICollection<MailAddress>

ICollection<MailAddress> collection = to.Split(',').Select(i => new MailAddress(i)).[ToList | ToArray]();