I would like to rewrite the following code in one line, using LINQ. Is it possible?
var to = new MailAddressCollection();
foreach(string recipient in recipients)
{
to.Add(new MailAddress(recipient));
}
Something like the following would be ideal. As written it returns multiple MailAddressCollections:
var to = recipients.Select(r => new MailAddressCollection() {new MailAddress(r)});
Note that these are framework classes, so I cannot rewrite MailAddressCollection to include a constructor argument.
MailAddressCollectiondoesn’t have a constructor that takes list of recipients. If you really want to do it in one line, you can write the following: