Combining consecutive dates into ranges

Posted by Ragha J on Stack Overflow See other posts from Stack Overflow or by Ragha J
Published on 2013-10-17T17:16:47Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 238

Filed under:
|
|

I have a List of objects

public class sample
{
 public DateTime Date;
 public string content;
}

I want to be able to create a list of new objects

public class sampleWithIntervals
{
 public DateTime startDate;
 public DateTime endDate;
 public string content;
}

The sample objects should be grouped into intervals based on the content. The intervals can include only those dates that are included in the original sample list. I dont know how to do this in Linq.

Sample data:

{"10/1/2013", "x"}
{"10/2/2013", "x"}
{"10/2/2013", "y"}
{"10/3/2013", "x"}
{"10/3/2013", "y"}
{"10/10/2013", "x"}
{"10/11/2013", "x"}
{"10/15/2013", "y"}
{"10/16/2013", "y"}
{"10/20/2013", "y"}

This should give me 
{"10/1/2013","10/3/2013", "x"}
{"10/2/2013","10/3/2013", "y"}
{"10/10/2013","10/11/2013", "x"}
{"10/15/2013","10/16/2013", "y"}
{"10/20/2013","10/20/2013", "y"}

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ