Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two stringlists and foreach

My WebAPI receives two strings, one containing the display values (names), one containing the hidden values (emails).

Before, it only received one string, over which it used a foreach, and I am unsure how to get it to work with two, how to fill "name" from "nameslist":

[System.Web.Http.HttpGet]
public AjaxAnswer BatchUserCreate(string email, string names) {
    string[] emaillist = email.Split('\n');
    string[] nameslist = names.Split('\n');
    foreach(string email in emaillist) {
        db.AddParameter("@email",email);
        db.AddParameter("@name",name);
        int newId = db.ExecuteScalar(userInsQuery);
    }
    return new AjaxAnswer(newId);
}
like image 752
Alexander Avatar asked Dec 09 '25 09:12

Alexander


1 Answers

Zip the two lists together

var nameEmailPairs = emaillist.Zip(namelist, (email,name)=>new{email,name});

You can then foreach over that, which will have items with a name and an email.

like image 139
Servy Avatar answered Dec 11 '25 22:12

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!