Multiple foreach for multiple list data

user3722851

I have 2 string list.

var justiceCourtName = formCollection["JusticeCourtName"];
var courtId = formCollection["CourtID"];
var justiceCourtNameList = justiceCourtName.Split(',');
var courtIdList = courtId.ToList();

justiceCourtNameList values below:

"New York"

"Paris"

courtIdList values below:

"10.33"

"43.15"

My question:

I need to use foreach for justiceCourtNameList and courtIdList after that , from justiceCourtNameList to Name (below) and from courtIdList to lawCourtId one by one.

But I do not know how can i set justiceCourtNameList and courtIdList one by one to new LawCourt ?

var lawCourt = new LawCourt { JusticeCourtID = lawCourtId, Name = lawCourtName };


ServiceLibraryHelper.LawServiceHelper.UpdateLawCourt(lawCourt); // Update
Thomas Levesque

If I understood your question correctly, what you want is the Zip extension method:

var results = courtIdList
                  .Zip(justiceCourtNameList,
                       (lawCourtId, lawCourtName) =>
                           new LawCourt
                           {
                               JusticeCourtID = lawCourtId,
                               Name = lawCourtName
                           )};

It enumerates the lists in parallel, and associates the current item from the first list with the current item from the second. The lambda expression specifies what to return in the output sequence, based on the pair of items from the input sequences.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related