私は2つの配列を持っています:
string[] fruit = { "apple", "banana", "lemon", "apple", "lemon" };
int[] quantity = { 2, 4, 1, 2, 2 };
2番目のものは最初のものと同じ長さであり、整数は各果物の量です。
これらの2つの配列を作成したいと思います。
totalefruit = { "apple", "banana", "lemon" };
totalquantity = {4, 4, 3}
これを試して:
string[] fruit = { "apple", "banana", "lemon", "apple", "lemon" };
int[] quantity = { 2, 4, 1, 2, 2 };
var result =
fruit
.Zip(quantity, (f, q) => new { f, q })
.GroupBy(x => x.f, x => x.q)
.Select(x => new { Fruit = x.Key, Quantity = x.Sum() })
.ToArray();
var totalefruit = result.Select(x => x.Fruit).ToArray();
var totalquantity = result.Select(x => x.Quantity).ToArray();
result
このように見えます:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加