There is a collection of items representing some readings on a specific day of the month:
public class MyValue { public int day; public double value; }
Suppose these items have collected the following collection:
var myList = new List<MyValue>(); myList.Add( new MyValue() { day = 6, value = 20 } ); //6-го апреля показание равно 20 myList.Add( new MyValue() { day = 6, value = 50 } ); //Еще одно показание за 6 апреля myList.Add( new MyValue() { day = 2, value = 15 } ); //Показание за 2 апреля
It’s impossible to figure out how to compile the readings for the same day in the following 2 ways:
- Leave only more
- Sum all readings for one day in one element;
I am writing through foreach
, selecting the list items one by one and comparing with all other elements in the nested foreach
, but then during the execution of the second task, the element is added to itself.
It seems to me there are better ways to solve this problem, please push in the right direction.
Maybe through LINQ somehow you can?
GroupBy
feature - GrundymyList
way, your example will not compile,myList
is a list, and you want to add it instead ofMyValue
, and private fields, and you set them in the initialization list, it will not work outside the class - Grundydouble
,int
is the day of the month. About the contradiction, probably poorly expressed in the question. It was meant that in one case you only need to choose more, in the other you need to sum up all the readings in one day. In any case, one item must remain in one day. - user200141