显示字符串中的字符串列表
Displaying a list of string in a string
好的,我在这里尝试向用户发送一封关于无效附件的电子邮件。我在之前的函数中收集了无效附件名称(字符串)的列表,然后我试图发送一封电子邮件,其中列出所有被视为无效的附件。不知道如何处理这种情况。我有以下示例
List<string> invalidType = new List<string>();
if (invalidType != null)
{
emailInvalidBody = new ItemBody
{
Content = " Expense attachements recieved with subject line [ "
+ email.Subject
+ " ] sent at [ "
+ email.DateTimeReceived.Value.DateTime.ToLocalTime()
+ " ] had the following invalid attachments ["
+ invalidType
+ "]. Please make sure all attachment's are images or PDF's.",
};
}
List<Recipient> emailInvalidRecipients = new List<Recipient>();
emailInvalidRecipients.Add(new Recipient
{
EmailAddress = new EmailAddress
{
Address = email.Sender.EmailAddress.Address
}
});
Message invalidEmailMessage = new Message
{
Subject = "Invalid Expenses",
Body = emailInvalidBody,
ToRecipients = emailInvalidRecipients
};
starBox.Users[EMAILBOX].SendMailAsync(invalidEmailMessage, false).Wait();
我的最终目标是拥有一个可以执行以下操作的电子邮件正文
"Expense attachements recieved with subject line Subject sent at 8:00 1/29/16 had the following invalid attachments [ invalidattachment1 invalidattachment2 invalidattachment3 invalidattachment4 ]. Please make sure all attachment's are images or PDF's.",
使用 string.Join 将您的 List<string>
转换为一个列表,所有项目都连接在一起
List<string> invalidType = new List<string>();
.....
// Probably you don't need to check for null but for element count.
// However it doesn't hurt to be a little defensive...
if (invalidType != null && invalidType.Count > 0)
{
string attachments = string.join(",", invalidType);
emailInvalidBody = new ItemBody
{
Content = " Expense attachments received with subject line [ " +
email.Subject + " ] sent at [ " +
email.DateTimeReceived.Value.DateTime.ToLocalTime() +
" ] had the following invalid attachments [" +
attachments + "]. Please make sure all " +
" attachment's are images or PDF's.",
};
}
您可以使用 Linq
Aggregate
Content = " Expense attachements recieved with subject line [ " +
email.Subject + " ] sent at [ " +
email.DateTimeReceived.Value.DateTime.ToLocalTime() +
" ]had the following invalid attachments ["
+ invalidType.Aggregate((i, j) => i + " " + j))
+ "]. Please make sure all attachment's are images or PDF's.",
好的,我在这里尝试向用户发送一封关于无效附件的电子邮件。我在之前的函数中收集了无效附件名称(字符串)的列表,然后我试图发送一封电子邮件,其中列出所有被视为无效的附件。不知道如何处理这种情况。我有以下示例
List<string> invalidType = new List<string>();
if (invalidType != null)
{
emailInvalidBody = new ItemBody
{
Content = " Expense attachements recieved with subject line [ "
+ email.Subject
+ " ] sent at [ "
+ email.DateTimeReceived.Value.DateTime.ToLocalTime()
+ " ] had the following invalid attachments ["
+ invalidType
+ "]. Please make sure all attachment's are images or PDF's.",
};
}
List<Recipient> emailInvalidRecipients = new List<Recipient>();
emailInvalidRecipients.Add(new Recipient
{
EmailAddress = new EmailAddress
{
Address = email.Sender.EmailAddress.Address
}
});
Message invalidEmailMessage = new Message
{
Subject = "Invalid Expenses",
Body = emailInvalidBody,
ToRecipients = emailInvalidRecipients
};
starBox.Users[EMAILBOX].SendMailAsync(invalidEmailMessage, false).Wait();
我的最终目标是拥有一个可以执行以下操作的电子邮件正文
"Expense attachements recieved with subject line Subject sent at 8:00 1/29/16 had the following invalid attachments [ invalidattachment1 invalidattachment2 invalidattachment3 invalidattachment4 ]. Please make sure all attachment's are images or PDF's.",
使用 string.Join 将您的 List<string>
转换为一个列表,所有项目都连接在一起
List<string> invalidType = new List<string>();
.....
// Probably you don't need to check for null but for element count.
// However it doesn't hurt to be a little defensive...
if (invalidType != null && invalidType.Count > 0)
{
string attachments = string.join(",", invalidType);
emailInvalidBody = new ItemBody
{
Content = " Expense attachments received with subject line [ " +
email.Subject + " ] sent at [ " +
email.DateTimeReceived.Value.DateTime.ToLocalTime() +
" ] had the following invalid attachments [" +
attachments + "]. Please make sure all " +
" attachment's are images or PDF's.",
};
}
您可以使用 Linq
Aggregate
Content = " Expense attachements recieved with subject line [ " +
email.Subject + " ] sent at [ " +
email.DateTimeReceived.Value.DateTime.ToLocalTime() +
" ]had the following invalid attachments ["
+ invalidType.Aggregate((i, j) => i + " " + j))
+ "]. Please make sure all attachment's are images or PDF's.",