Rule based grouping of data into segments LINQ

I need to group data into segments based on some preset rules. I have input data taken in 5 minute intervals and sometimes more than 5 minutes. I need to put them into segments for further processing, but it does not work for me for some reason.

Example of data:

11.01.2017 14:59:19 +01:00   value 15
11.01.2017 15:04:19 +01:00   value 17
11.01.2017 15:09:19 +01:00   value 14
11.01.2017 15:14:19 +01:00   value 11
11.01.2017 15:19:19 +01:00   value 0
11.01.2017 15:24:19 +01:00   value 8
---------here I want divide---------
11.01.2017 15:49:19 +01:00   value 6
11.01.2017 15:54:19 +01:00   value 14
11.01.2017 15:59:19 +01:00   value 5
11.01.2017 16:04:19 +01:00   value 3
11.01.2017 16:09:19 +01:00   value 5

Here is the example code: http://rextester.com/GQW59118

There should be two segments in the example.

I need something like this

var grouped = dataList.GroupBy(tv => /*group when is dataList[i]-dataList[i-1]==5 */)
                .Select(g => g.OrderBy(tv => tv.dateTime).ToList()
                ).ToList();

Can anyone please advise? (without if in loop)

1 answer

  • answered 2017-01-11 14:18 VDN

    Try this:

    public class Program
    {
        public static void Main(string[] args)
        {
             List<TimeValue> ttcaList = new List<TimeValue>();
    
            Random s = new Random();
            DateTimeOffset dto = DateTimeOffset.Now;  
    
    
            for (int i = 0; i < 6; i++)
            {
                ttcaList.Add(new TimeValue(dto.AddMinutes(i*5), s.Next(0, 20)));
            }
            for (int i = 0; i < 5; i++)
            {
                ttcaList.Add(new TimeValue(dto.AddMinutes(10*5).AddMinutes(i*5), s.Next(0, 20)));
            }
    
    
            foreach (var item in ttcaList)
            {
                Console.WriteLine(item.Time+"   value "+ item.Value);
            }
    
            List<List<TimeValue>> grouped = new List<List<TimeValue>>();
    
            var interval = 5;
            DateTimeOffset lastTime = new DateTimeOffset();
    
            foreach (var item in ttcaList)
            {
    
                if (grouped.Count == 0) 
                {
                    grouped.Add(new List<TimeValue>() {item});
                    lastTime = item.Time;
                }
                else 
                {
                    if ((item.Time - lastTime).TotalMinutes <= interval)
                        grouped.Last().Add(item);
                    else 
                        grouped.Add(new List<TimeValue>() {item});
    
                    lastTime = item.Time;
                }
            }
    
            Console.WriteLine("\n\n grouped data");
    
            foreach (var skup in grouped)
            {
                Console.WriteLine("\t\t group");
            foreach (var item in skup)
            {
    
                Console.WriteLine(item.Time+"   value "+ item.Value);
             }
            }
        }
    

    There you have time interval 5 minutes, you may change it by changing the value of interval