我在我的应用程序上使用SparkPost向我和客户发送电子邮件。为了做到这一点,我需要使用C#序列化一个数组。我有下面的代码似乎没有工作,我不知道为什么。LINQ在自定义类型的列表中选择?
recipients = new List<Recipient>() {
toAddresses.Select(addr => new Recipient() {
address = addr.ToString()
})
}
toAddresses
只是一个List<string>
与电子邮件地址。
收件人类:
class Recipient {
public string address;
}
是LINQ选择的输出应该是这样的:
recipients = new List<Recipient>(){
new Recipient() {
address ="[email protected]"
},
new Recipient() {
address ="[email protected]"
},
new Recipient() {
address ="[email protected]"
},
new Recipient() {
address ="[email protected]"
}
}
任何帮助将是巨大的,谢谢!
特定错误:
Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable' to 'app.Recipient'
Error CS1950 The best overloaded Add method 'List.Add(Recipient)' for the collection initializer has some invalid arguments
请求字符串:
wc.UploadString("https://api.sparkpost.com/api/v1/transmissions", JsonConvert.SerializeObject(
new {
options = new {
ip_pool = "sa_shared"
},
content = new {
from = new {
name = "a Sports",
email = "[email protected]"
},
subject = subject,
html = emailBody
},
recipients = new List<Recipient>() {
toAddresses.Select(addr => new Recipient() {
address => addr
})
}
}
));
请问您可以用什么方式描述您当前的代码“似乎没有工作?”你是否收到错误信息?与您预期的不同的输出? – StriplingWarrior
@StriplingWarrior查看更新后的问题。 –