Q:

SQL : Linq - Different rows with change in column value

How can you make a filter in LINQ, where a query groups only two subsequent rows, if their Level column is ascended by one.
This is what I have at this moment:
var alert = db.Logs.OrderBy(u => u.Time).GroupBy(r => r.EquipmentNr).Where(s => s.Count() > 1);

However it takes all the rows it does not find those, which are different in the Level column, and they must be different over time. There must be rows next to each other, ordered by time.
Or should I create a grouping class and manually, run through the query I already have and add whether I find them?
public class Log
{
    public int Id { get; set; }
    public DateTime Time { get; set; }
    public Shift Shift { get; set; }
    public int EquipmentNr { get; set; }
    public int OrderNr { get; set; }
    public bool SupervisorCalled { get; set; }
    public string Issue { get; set; }
    public string Repairs { get; set; }
    public string Responsible { get; set; }
    public Level Level { get; set; }
}

A:

Most of the times, for sequential processing where elements in a list are somehow related, a classic foreach is much better than LINQ. Often it can't even be done by LINQ. However, in this case only successive elements are to be compared, so Aggregate can be used.
The grouping condition is: level(n) - level(n-1) = 1. So we should add identical grouping keys to each element in the list meeting this condition. Here's a way to do that (using Linqpad). Each element is stored in a Tuple where Item1 is the grouping key. Then the tuples are grouped by these keys:
void Main()
{
    var list = new List<Equipment>
    { 
    new Equipment{Level = 11},
    new Equipment{Level = 1},
    new Equipment{Level = 18},
    new Equipment{Level = 0},
    new Equipment{Level = 6},
    new Equipment{Level = 4},
    new Equipment{Level = 5},
    new Equipment{Level = 20},
    new Equipment{Level = 9},
    new Equipment{Level = 14},
    new Equipment{Level = 12},
    new Equipment{Level = 17},
    new Equipment{Level = 2},
    new Equipment{Level = 13},
    new Equipment{Level = 15},
    };
    list.OrderBy(eq => eq.Level)
        .Aggregate
        (
            new List<Tuple<int,Equipment>>(), // Seed the list of tuples
            (tuples,eq) =>
            {
                // The grouping condition: compare to the previous item
                // (if present) and use its Level as grouping key if the
                // condition is met.
                var key = tuples.Any() 
                            && eq.Level - tuples.Last().Item2.Level == 1
                        ? tuples.Last().Item1
                        : eq.Level; 
                tuples.Add(Tuple.Create(key, eq)); 
                return tuples;
            }, 
            tuples => tuples.GroupBy (tuple => tuple.Item1)
        // Done! Now just a Select for display
        ).Select
        (tupleGroup =>
            new { 
                    tupleGroup.Key,
                    Numbers = string.Join(",", tupleGroup
                                               .Select(e => e.Item2.Level))
                }
        ). Dump();

}

// Define other methods and classes here
class Equipment
{
    public int Level { get; set; }
}

The output:
Key  Numbers 
---  --------------
0    0,1,2 
4    4,5,6 
9    9 
11   11,12,13,14,15 
17   17,18 
20   20 

