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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



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


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.


cow id


01-08-2018 to 04-08-2018



Please give me some idea how to do this.





how about TakeWhile?
– vasily.sib
Aug 13 at 6:00






@vasily.sib 01-08-2018 to 04-08-2018 is for example. I don't have any static dates. I have to find those dynamically from list and then divide from there.
– jignesh patel
Aug 13 at 6:04



01-08-2018 to 04-08-2018





I mean: list.TakeWhile(x => x.DateOfMilking - previousDateOfMilking < TimeSpan.FromDays(3)).ToList()
– vasily.sib
Aug 13 at 6:10



list.TakeWhile(x => x.DateOfMilking - previousDateOfMilking < TimeSpan.FromDays(3)).ToList()





@vasily.sib how we will get previousDateOfMilking
– jignesh patel
Aug 13 at 6:13


previousDateOfMilking





its up to you. At first like list[0].DateOfMilking, then inside TakeWhile. You are asking for "some idea" and not for complete working solution, don't you?
– vasily.sib
Aug 13 at 6:15


list[0].DateOfMilking


TakeWhile




1 Answer
1



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;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard