Reputation: 20234
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);
}
Upvotes: 2
Views: 85
Reputation: 186803
Try changing foreach loop to for loop:
[System.Web.Http.HttpGet]
public AjaxAnswer BatchUserCreate(string email, string names) {
string[] emaillist = email.Split('\n');
string[] nameslist = names.Split('\n');
// You should declare "newId" somewhere here
// if you want to return it via "new AjaxAnswer(newId)"
int newId = 0;
// if emaillist and nameslist have diffrent lengths
// let's take minimal length
int n = Math.Min(nameslist.Length, emaillist.Length);
for (int i = 0; i < n; ++i) {
db.AddParameter("@email", emaillist[i]);
db.AddParameter("@name", nameslist[i]);
newId = db.ExecuteScalar(userInsQuery);
}
return new AjaxAnswer(newId);
}
Upvotes: 1
Reputation: 1737
try this work around hope it will help
for(int i =0 ; i< emaillist .count; i++)
{
db.AddParameter("@email",emaillist[i]);
db.AddParameter("@name",nameslist[i]);
int newId = db.ExecuteScalar(userInsQuery);
}
Upvotes: 1
Reputation: 14432
If the length of both arrays (emaillist & nameslist) is equal, you should use a for loop instead of a foreach loop:
[System.Web.Http.HttpGet]
public AjaxAnswer BatchUserCreate(string email, string names) {
string[] emaillist = email.Split('\n');
string[] nameslist = names.Split('\n');
for(int i = 0; i < emaillist.Length; i++) {
db.AddParameter("@email",emaillist[i]);
db.AddParameter("@name",namelist[i]);
int newId = db.ExecuteScalar(userInsQuery);
}
return new AjaxAnswer(newId);
}
Upvotes: 1
Reputation: 12789
You should use a for
loop instead, something like this could help:
[System.Web.Http.HttpGet]
public AjaxAnswer BatchUserCreate(string email, string names) {
string[] emaillist = email.Split('\n');
string[] nameslist = names.Split('\n');
for(int i = 0; i!=emaillist.Length; ++i) {
db.AddParameter("@email", emaillist[i]);
db.AddParameter("@name", nameslist.Length > i ? nameslist[i] : "No name");
int newId = db.ExecuteScalar(userInsQuery);
}
return new AjaxAnswer(newId);
}
Upvotes: 1
Reputation: 56716
If you have a guarantee that these lists are in same order - use plain for loop:
for(int i=0; i<emaillist.Length; i++) {
string email = emaillist[i];
string name = namelist[i];
...
Or you can use LINQ with Zip
:
var records = emaillist.Zip(namelist, (email, name) => new {email, name});
foreach(var r in records) {
string email = r.email;
string name = r.name;
...
Upvotes: 1
Reputation: 203828
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.
Upvotes: 5