3

I'm using SparkPost on my application to send emails to me and clients. In order to do this, I need to serialize an array using C#. I have the following code that does not seem to be working and I have no idea why.

recipients = new List<Recipient>() {
    toAddresses.Select(addr => new Recipient() {
        address = addr.ToString()
    })
}

toAddresses is just a List<string> with email addresses.

Recipient class:

class Recipient {
    public string address;
}

The output of that LINQ select should look like this:

recipients = new List<Recipient>(){
    new Recipient() {
        address ="[email protected]"
    },
    new Recipient() {
        address ="[email protected]"
    },
    new Recipient() {
        address ="[email protected]"
    },
    new Recipient() {
        address ="[email protected]"
    }
}

Any help would be great, thanks!

Specific Errors:

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

Request String:

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
        })
    }
}

));

2
  • Can you please describe in what way your current code "does not seem to be working?" Are you getting an error message? Different output than you'd expected? Commented Apr 17, 2017 at 14:54
  • @StriplingWarrior See updated question. Commented Apr 17, 2017 at 14:58

1 Answer 1

7

Seems like you need simple mapping

var recipients = toAddresses.Select(addr => new Recipient { address = addr }).ToList();

You cannot use IEnumerable as parameter for list initialization

var recipients = new List<Recipient>() { toAddresses.Select...  }

Initialization logic will call List.Add on every item you pass in { }, so it expects instances of Recepient separated by comma, but when you pass there IEnumerable it fail.

List<T> have overload constructor which accept IEnumerable<T> as argument so you can use this

var recepients = new List<Recepient>(toAddresses.Select(addr => new Recipient {address = addr}));

But on my own opinion simple mapping seems more readable.

var message = new 
{
    options = new 
    { 
        ip_pool = "sa_shared"
    },
    content = new 
    {
        from = new 
        {
            name = "a Sports",
            email = "[email protected]"
        },
        subject = subject,
        html = emailBody
    },
    recipients = toAddresses.Select(addr => new Recipient() { address = addr}).ToList()
}
Sign up to request clarification or add additional context in comments.

9 Comments

Why would this work when his existing code doesn't work?
@StriplingWarrior: because when the List<Recipient> constructor is invoked, the compiler expects the LINQ query to result in a single recipient. It gives an error: Argument #1' cannot convert System.Collections.Generic.IEnumerable<Recipient>' expression to type Recipient'`.
@WillemVanOnsem I think the point was that the reason should be part of the answer.
Good edit. You may also want to mention that he could have passed the Select result into the constructor of the new List<Recipient().
@AndriiLitvinov: I only said that because I think that's what the OP probably thought he was doing. The fact that he was newing up a List and trying to pass values through the initializer makes me think he'd seen a similar approach before (using the constructor argument) and was just getting confused between the two syntaxes. Fabio did a great job with this answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.