Compare two consecutive lines - by

I have a model below

ID     Date                    BoitierNumber
1      07/04/2012 14:01:46     1
2      07/04/2012 14:01:50     2
3      07/04/2012 14:01:50     3
4      07/04/2012 14:01:56     1
5      07/04/2012 14:02:06     1
6      07/04/2012 14:02:10     2

      

I have grouped the rows using (BoitierNumber)

*boitier Number 1

 1      07/04/2012 14:01:46
 4      07/04/2012 14:01:56 
 5      07/04/2012 14:02:06

*boitier Number 2

 2      07/04/2012 14:01:50
 6      07/04/2012 14:02:10 

*boitier Number 3

 3      07/04/2012 14:01:50

      

For this i used this code

var groups = context.Essais.GroupBy(p => p.BoitierNumber)
                .Select(g => new { GroupName = g.Key, Members = g });

foreach (var g in groups)
{
    Console.WriteLine("Members of {0}", g.GroupName);

    foreach (var member in g.Members.OrderBy(x=>x.Id))
    {
        Console.WriteLine("{0}     {1}", member.Id,member.Date);
    }
}

      

At the moment everything is working fine now I want to compare the date of two consecutive grouped rows if row [i] .date> row [i-1] .date I will delete row [i-1]

For example:

*boitier Number 1

 1      07/04/2012 14:01:46
 4      07/04/2012 14:01:56 
 5      07/04/2012 14:02:06 
 8      07/04/2012 14:01:00 
 10     07/04/2012 14:00:00 
 13     07/04/2012 14:03:00 
 boitier Number 1 
 ---> Date of row of Id 4 > Date of row of ID 1
 then i will delete row of ID 1
 ---> Date of row of Id 5 > Date of row of ID 4
 then i will delete row of ID 4
 ---> Date of row of Id 8 < Date of row of ID 5
 then i will skip it
 ---> Date of row of Id 10 < Date of row of ID 8
 then i will skip it
 ---> Date of row of Id 13 > Date of row of ID 10
 then i will delete row of ID 10
 ...

 TherFore, After this process, only rows 13 and 8 will be remain 

      

+3


source to share


1 answer


The fastest change you can make now:

var groups = context.Essais.GroupBy(p => p.BoitierNumber)
                .Select(g => new 
                                { 
                                  GroupName = g.Key, 
                                  Members = g.OrderBy(m=>m.Id)
                                });

      

EDIT EF doesn't seem to work with calling ToList () in projection.

Members will now be an ordered list by id, then you can do instead of for foreach



foreach (var g in groups)
{
    var members = g.Members.ToList();
    for (int i = 1, i < members.Count; i++)
    {
         var previousMember = members[i-1];
         var currentMember = members[i];
         if (..)
             // code to delete
    }
}

      

Just notice. By projecting the grouped members with the original query, it will create other queries to select the members that match each group key. You will load the entire table anyway, but in subsequent queries. You can do the grouping in memory:

var groups = context.Essais
                      .AsEnumberable().
                      .GroupBy(p => p.BoitierNumber)
                      .Select(g => new { GroupName = g.Key, Members = g.OrderBy(m=>m.Id).ToList()  });

      

+3


source







All Articles