Convert list to dictionary and summarize values ​​using linq

I have a list i.e. List<Field> . This Field class contains code and value properties among other fields, and I would like to be able to use linq to summarize all values ​​for the same code.

I know that I could scroll through the list and add it to the dictionary using the following code, but I'm sure there is a cleaner way to do this:

 if (totals.ContainsKey(code)) { totals[code] += value; } else { totals.Add(code, value); } 

Any ideas?

I found something similar, but this applies to list>, which is not what I have:

 var result = Sales.SelectMany(d => d) // Flatten the list of dictionaries .GroupBy(kvp => kvp.Key, kvp => kvp.Value) // Group the products .ToDictionary(g => g.Key, g => g.Sum()); 

from this article [Amount using Linq in <List<Dictionary<string, int>> ] Amount using Linq in <List <Dictionary <string, int →>

Any ideas? I could always change my code to have Dictionary<string, Field> , but I'm sure there must be a way to do this with a list and linq.

Thanks.

I have a list i.e. List<Field> . This Field class contains code and value properties among other fields, and I would like to be able to use linq to summarize all values ​​for the same code.

I know that I could scroll through the list and add it to the dictionary using the following code, but I'm sure there is a cleaner way to do this:

 if (totals.ContainsKey(code)) { totals[code] += value; } else { totals.Add(code, value); } 

Any ideas?

I found something similar, but this applies to list>, which is not what I have:

 var result = Sales.SelectMany(d => d) // Flatten the list of dictionaries .GroupBy(kvp => kvp.Key, kvp => kvp.Value) // Group the products .ToDictionary(g => g.Key, g => g.Sum()); 

from this article [Amount using Linq in <List<Dictionary<string, int>> ] Amount using Linq in <List <Dictionary <string, int →>

Any ideas? I could always change my code to have Dictionary<string, Field> , but I'm sure there must be a way to do this with a list and linq.

Thanks.

UPDATE:

Sorry, I think I skipped an important section regarding the above. The list is contained in another list, namely: List> myitemList; which will contain other irrelevant fields that may require further filtering. I'm not sure???

NOTE. Sorry, formatting is messed up again!

To give a little context to this:

Item1 (type list)

Item Name Value

(Clause 1) Type 1 (Clause 2) Description Test (Clause 3) Code A (Clause 4) Net 100.00

Item 2 (type list)

Item Name Value

(Clause 1) Type 2 (Clause 2) Description Test1 (Clause 3) Code B (Clause 4) Network 95.55

Item 3 (type list)

Item Name Value

(Clause 1) Type 2 (Clause 2) Description Test2 (Clause 3) Code A (Clause 4) Net 35.95

As you can see, each list of List types contains 4 fields in which my field is defined with a name (String) and a value (Object)

Each of these lists is then added to the main list. Therefore, I need to iterate over the main list and, in turn, I want to get a dictionary that will contain a "Code" and the amount of "Net" for each list. So in the end I should just finish

<p> A, 135.95 B, 95.55

I don't know if that makes sense. I hope so!

UPDATE

The fact that I'm dealing with a list> has not really changed, since I actually wanted to summarize one list at a time, so give the answer correctly! Thanks!

+6
source share
3 answers

The code you posted is almost what you need - to use it in the list, you need to simplify it a bit:

 var result = myList // No flattening .GroupBy(x => x.Code) // Group the items by the Code .ToDictionary(g => g.Key, g => g.Sum(v => v.Value)); // Total up the values 
+7
source

You can do:

 Dictionary<string, int> dictionary = list.GroupBy(r => r.ID) .ToDictionary(grp => grp.Key, grp => grp.Sum(r => r.Value)); 

Given that you have a class:

 public class MyClass { public string ID { get; set; } public int Value { get; set; } } 
+3
source
 var list = new List<Field> { new Field { Code = "A", Value = 10 }, new Field { Code = "A", Value = 20 }, new Field { Code = "B", Value = 30 }, }; var dic = list .GroupBy(z => z.Code) .ToDictionary(z => z.Key, z => z.Sum(f => f.Value)); 
+2
source

Source: https://habr.com/ru/post/957442/


All Articles