質問: 0; i < loopCount; i++) { switch (NoOfRows) { case 1: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); break; case 2: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); break; case 3: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); break; case 4: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); break; case 5: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); break; case 6: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); if (InputList6.Count > i) ExpectedValues.Add(InputList6[i]); break; case 7: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); if (InputList6.Count > i) ExpectedValues.Add(InputList6[i]); if (InputList7.Count > i) ExpectedValues.Add(InputList7[i]); break; case 8: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); if (InputList6.Count > i) ExpectedValues.Add(InputList6[i]); if (InputList7.Count > i) ExpectedValues.Add(InputList7[i]); if (InputList8.Count > i) ExpectedValues.Add(InputList8[i]); break; case 9: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); if (InputList6.Count > i) ExpectedValues.Add(InputList6[i]); if (InputList7.Count > i) ExpectedValues.Add(InputList7[i]); if (InputList8.Count > i) ExpectedValues.Add(InputList8[i]); if (InputList9.Count > i) ExpectedValues.Add(InputList9[i]); break; case 10: if (InputList1.Count > i) ExpectedValues.Add(InputList1[i]); if (InputList2.Count > i) ExpectedValues.Add(InputList2[i]); if (InputList3.Count > i) ExpectedValues.Add(InputList3[i]); if (InputList4.Count > i) ExpectedValues.Add(InputList4[i]); if (InputList5.Count > i) ExpectedValues.Add(InputList5[i]); if (InputList6.Count > i) ExpectedValues.Add(InputList6[i]); if (InputList7.Count > i) ExpectedValues.Add(InputList7[i]); if (InputList8.Count > i) ExpectedValues.Add(InputList8[i]); if (InputList9.Count > i) ExpectedValues.Add(InputList9[i]); if (InputList10.Count > i) ExpectedValues.Add(InputList10[i]); break; } } ```
回答: What about some Linq? ``` var allInputLists = new List> { InputList1, InputList2, InputList3, InputList4, InputList5, InputList6, InputList7, InputList8, InputList9, InputList10 }; var expectedValues = from i in Enumerable.Range(0, loopCount) from list in allInputLists.Take(NoOfRows - 1) where list.Count > i select list[i]; ``` Or, in fluent syntax: ``` var expectedValues = Enumerable.Range(0, loopCount) .SelectMany(i => allInputLists.Take(NoOfRows - 1) .Where(list => list.Count > i) .Select(list => list[i]) ); ``` Although the query syntax in this case is a bit nicer.