我在我的應用程序上使用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查看更新後的問題。 –