Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用LINQ C#从字符串列表向电子邮件添加附件?_C#_Linq - Fatal编程技术网

如何使用LINQ C#从字符串列表向电子邮件添加附件?

如何使用LINQ C#从字符串列表向电子邮件添加附件?,c#,linq,C#,Linq,我必须将电子邮件附件添加到邮件中,我使用foreach进行了添加,但我想用LINQ替换它 foreach(string attachment in email.Attachments) { mailMessage.Attachments.Add(new Attachment(attachment)); } 附件是一个字符串列表 我试过: mailMessage.Attachments= email.Attachments.Aggregate(new At

我必须将电子邮件附件添加到邮件中,我使用foreach进行了添加,但我想用LINQ替换它

foreach(string attachment in email.Attachments)
     {
        mailMessage.Attachments.Add(new Attachment(attachment));
     }
附件是一个字符串列表

我试过:

  mailMessage.Attachments= email.Attachments.Aggregate(new AttachmentCollection(), (c, r) => { c.Add(new Attachment(r)); return c; });
但有以下错误:

类型“System.Net.Mail.AttachmentCollection”没有构造函数 明确的

我不知道怎么做


任何通知?

附件都属于AttachmentCollection类型。 AttachmentCollection派生自Collection,位于Collection类的metdata下面

public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
    public Collection();
    public Collection(IList<T> list);

    public T this[int index] { get; set; }

    public int Count { get; }
    protected IList<T> Items { get; }

    public void Add(T item);
    public void Clear();
    public bool Contains(T item);
    public void CopyTo(T[] array, int index);
    public IEnumerator<T> GetEnumerator();
    public int IndexOf(T item);
    public void Insert(int index, T item);
    public bool Remove(T item);
    public void RemoveAt(int index);
    protected virtual void ClearItems();
    protected virtual void InsertItem(int index, T item);
    protected virtual void RemoveItem(int index);
    protected virtual void SetItem(int index, T item);
}

我不清楚你为什么会在这样的问题上投反对票,可能是因为你没有发表任何问题或错误,这表明你自己在尝试一些东西。所以,也许可以提供更多的信息,你想实现什么,你已经尝试了什么,你的问题是什么…谢谢你解释我,我真的不明白我的问题出了什么问题。我会按照你的建议马上编辑它。非常感谢。没问题。就我个人而言,我投赞成票,谢谢你的解释,这很有帮助。那么,这是否意味着无法使用LINQ向电子邮件添加附件?正确,这是不可能的。OK,我将其更改为for循环,这样我可以轻松设置附件的名称。。。非常感谢你的帮助@玛丽,没问题,很高兴能帮上忙
public class MailMessage : IDisposable
{
    public MailMessage();
    public MailMessage(string from, string to);
    public MailMessage(MailAddress from, MailAddress to);
    public MailMessage(string from, string to, string subject, string body);

    public AlternateViewCollection AlternateViews { get; }
    public AttachmentCollection Attachments { get; }
    public MailAddressCollection Bcc { get; }
}