divide list in to sub list if there is no entry for four days continuously

c# entity-framework-core linq

Question

I have one list which has three columns. those are CowID,DateOfMilking,NumberOfMilkInLtr.

Now I filter the list by cow id. I want to divide the filtered list in sub list from all data if there are no entries for four dates continuously. For example, if there is no entry for the dates from 01-08-2018 to 04-08-2018 then divide list from here and so on.

Please give me some idea how to do this.

1
0
8/13/2018 8:24:55 AM

Accepted Answer

Here's a start to a possible solution. Note that this unfortunately removes the 1st item, which will need to be resolved. But other than that, it does split the lists based on the 4 day gap

void Main()
{
    var data = new List<CowOutput> {
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 7, 30), NumberOfMilkInLtr = 1.2F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 7, 31), NumberOfMilkInLtr = 1.3F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 8,  5), NumberOfMilkInLtr = 1.5F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 8,  6), NumberOfMilkInLtr = 1.1F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 8,  7), NumberOfMilkInLtr = 1.7F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 8,  8), NumberOfMilkInLtr = 1.4F },
        new CowOutput {CowID = 2, DateOfMilking = new DateTime(2018, 7, 30), NumberOfMilkInLtr = 1.4F },
        new CowOutput {CowID = 3, DateOfMilking = new DateTime(2018, 7, 31), NumberOfMilkInLtr = 1.1F },
        new CowOutput {CowID = 2, DateOfMilking = new DateTime(2018, 8,  1), NumberOfMilkInLtr = 1.8F },
        new CowOutput {CowID = 5, DateOfMilking = new DateTime(2018, 8,  2), NumberOfMilkInLtr = 1.4F },
        new CowOutput {CowID = 6, DateOfMilking = new DateTime(2018, 8,  3), NumberOfMilkInLtr = 1.2F },
        new CowOutput {CowID = 2, DateOfMilking = new DateTime(2018, 8,  5), NumberOfMilkInLtr = 1.5F },
        new CowOutput {CowID = 1, DateOfMilking = new DateTime(2018, 6,  5), NumberOfMilkInLtr = 1.5F },
    };

    var cowId = 1;
    var days = 4;


    var cowData = data
        .Where(d => d.CowID == cowId)
        .OrderBy(d => d.DateOfMilking);

    cowData
        .Zip(
            cowData.Skip(1), 
            (current, next) =>
            {
                next.Group = (next.DateOfMilking.Subtract(current.DateOfMilking).TotalDays >= days)
                    ? current.Group + 1
                    : current.Group ;
                return next;
            })
        .GroupBy(d => d.Group)
        .Select(d => d.ToList())
        .ToList()
        .Dump(); // I'm using LinqPad to test this. You can remove this and just use the output.
}

public class CowOutput
{
    public Int32 CowID { get; set; }
    public DateTime DateOfMilking { get; set; }
    public Single NumberOfMilkInLtr { get; set; }
    public Int32 Group { get; set; }
}
1
8/13/2018 5:36:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow