[英]Is there a way to sum of elements in 2 elements into a new list in c#
我有兩個相同計數的不同列表正在嘗試總結為一個新列表
List<int> Calculate = new List<int>();
List<int> new_Quantity= new List<int>();
List<int> Qty= new List<int>();
所以讓 new_Quantity 假設 int 值為 [2,3,4],而 Qty 的 int 值為 [2,4,5]。 所以我希望新列表能夠計算 [4,7,9] 這是迄今為止使用的
For each(var i in new_Quantity)
{
for each(var j in Qty)
{
calculate.add( i + j);
}
}
但它沒有得到我想要的確切計算,因為這個 output 是 [4,7,9,4,7,9,4,7,9]; 那么我該如何解決呢?
制作 3 個列表:
static List<int> List1 = new List<int>();
static List<int> List2 = new List<int>();
static List<int> List3 = new List<int>();
void Sample()
{
List1.Add(2);
List2.Add(3);
for(int i=0;i<List1.Count();i++)
{
List3.Add(List1[i] + List2[i]);
}
}
結果,您已在 List3 中進行了總結
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.