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:

  1. Leave only more
  2. 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 - Grundy
  • myList way, your example will not compile, myList is a list, and you want to add it instead of MyValue , and private fields, and you set them in the initialization list, it will not work outside the class - Grundy
  • Modified the modifiers in the question and the creation of the list also made as needed - user200141
  • For readings I use double , 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
  • @Bald This code is used to build a graph of statistics for the month. X is the day of the month, y is the value. There are 2 types. If to simplify greatly, then the first is like payments, that is, in the morning they paid 100, in the evening 200, that means they paid 300 a day, and the second is like the meter readings. In the morning it was 10, in the evening it was 20. That means the device showed 20 all day. - user200141

3 answers 3

For grouping, you can use the GroupBy function, example

 var myList = new List<MyValue>(); myList.Add(new MyValue() { day = 6, value = 20 }); myList.Add(new MyValue() { day = 6, value = 50 }); myList.Add(new MyValue() { day = 2, value = 15 }); var groupped = (from my in myList group my.value by my.day into g select new MyValue { day = g.Key, value = g.Max() }).ToList(); 
  • @Bald, I would suggest that these are two different requests. for the first one is quite suitable, for the second you just need Max on Sum to replace - Grundy
 public class MeterReading { public int Day {get;set;} public double Value {get; set;} } var results = meterReadings .GroupBy(x=>x.Day) .Select(x=> new MeterReading{ Day = x.Key, Value = x.Max(v=>v.Value) }) .ToList(); foreach(var r in results) { Console.WriteLine(string.Format("{0} {1}",r.Day, r.Value)); } 

to get the sum of the value, change x.Max to x.Sum()
example

     var sum = myList.GroupBy(g => g.day).Select(g => g.Sum(i => i.value)); var big = myList.GroupBy(g => g.day).Select(g => g.Max(i => i.value));