LINQ query — Data aggregation (Group Adjacent)

前端 未结 7 2040
失恋的感觉
失恋的感觉 2020-12-03 03:09

Let\'s take a class called Cls:

public class Cls
{
    public int SequenceNumber { get; set; }
    public int Value { get; set; }
}
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 03:45

    MoreLinq provides this functionality out of the box

    It's called GroupAdjacent and is implemented as extension method on IEnumerable:

    Groups the adjacent elements of a sequence according to a specified key selector function.

    enumerable.GroupAdjacent(e => e.Key)
    

    There is even a Nuget "source" package that contains only that method, if you don't want to pull in an additional binary Nuget package.

    The method returns an IEnumerable>, so its output can be processed in the same way output from GroupBy would be.

提交回复
热议问题