38
1
using System;
2
using System.Linq;
3
using System.Collections.Generic;
4
5
//https://stackoverflow.com/questions/67378957/c-sharp-splitting-a-liststring-value
6
public class Program
7
{
8
public static void Main()
9
{
10
List<string> myList = new List<string>();
11
myList.Add("1 120 12");
12
myList.Add("1 130 22");
13
myList.Add("2 110 21");
14
myList.Add("2 100 18");
15
var query = myList.Select(a => a.Split(' ').Select(int.Parse).ToArray())
16
.GroupBy(
17
index => index[0],
18
amount => new
19
{
20
First = amount[1],
21
Second = amount[2]
22
},
23
(index, amount) => new
24
{
25
Index = index,
26
SumFirst = amount.Sum(a => a.First),
27
SumSecond = amount.Sum(a => a.Second)
28
}
29
);
30
31
foreach (var result in query)
32
{
33
Console.WriteLine(result.Index + " " + result.SumFirst + " " + result.SumSecond);
34
}
35
}
36
}
37
38
Cached Result
{"nameNom":"Test","nameNom2":"Test2"}
Name:
Name: Test2
Name:
Name: Test2