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)
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)
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!