user584018 user584018 - 2 months ago 18
C# Question

group data based on Time

I have one config list like below,

public class Config
{
public string CMasterName { get; set; }
public string cChildName { get; set; }
public int Hours { get; set; }
}

List<Config> config = new List<Config>(){
new Config { CMasterName ="MasterName1", cChildName="ChildName1", Hours=5}
};


"5" hrs group for "MasterName1" & "ChildName1"

Now I have PointData list with below data,

public class PointData
{
public int DataId { get; set; }
public string DataDescription { get; set; }
public DateTime InsertDateTime { get; set; }
public string MasterName { get; set; }
public string ChildName { get; set; }
}

List<PointData> pointData = new List<PointData>(){
new PointData { DataId = 1, DataDescription="Desc 1", InsertDateTime = new DateTime(2016, 9, 8, 7, 10, 24), MasterName ="MasterName1", ChildName="ChildName1"},
new PointData { DataId = 2, DataDescription="Desc 2", InsertDateTime = new DateTime(2016, 9, 8, 8, 10, 24), MasterName ="MasterName1", ChildName="ChildName1"},
new PointData { DataId = 3, DataDescription="Desc 3", InsertDateTime = new DateTime(2016, 9, 8, 10, 10, 24), MasterName ="MasterName1", ChildName="ChildName1"},
new PointData { DataId = 4, DataDescription="Desc 1", InsertDateTime = new DateTime(2016, 9, 8, 14, 10, 24), MasterName ="MasterName1", ChildName="ChildName1"}
};


Here, first 3 datapoints are within 5 hrs time range for "MasterName1" & "ChildName1" and last datapoints are beyond 5 hrs time

Now I would to match "datapoints" list with "config" list in a such way that first 3 records of "datapoints" will convert to 1 record and 4th record of "datapoints" will be a separate record.

Please suggest!!!

The result should be a list with one more column "count", like below,
enter image description here

Answer
DateTime lowDate = pointData.First().InsertDate;
foreach(PointData pD in pointdata)
        {
            DateTime test = pD.InsertDate;
            List<PointData> subList = pointData.Where(p => p.InsertDate > lowDate && <= test.AddHours(5));
        }
Comments