Calculate difference from previous item with LINQ

后端 未结 7 551
你的背包
你的背包 2020-11-27 14:50

I\'m trying to prepare data for a graph using LINQ.

The problem that i cant solve is how to calculate the \"difference to previous.

the result I expect is <

7条回答
  •  日久生厌
    2020-11-27 15:14

    One option (for LINQ to Objects) would be to create your own LINQ operator:

    // I don't like this name :(
    public static IEnumerable SelectWithPrevious
        (this IEnumerable source,
         Func projection)
    {
        using (var iterator = source.GetEnumerator())
        {
            if (!iterator.MoveNext())
            {
                 yield break;
            }
            TSource previous = iterator.Current;
            while (iterator.MoveNext())
            {
                yield return projection(previous, iterator.Current);
                previous = iterator.Current;
            }
        }
    }
    

    This enables you to perform your projection using only a single pass of the source sequence, which is always a bonus (imagine running it over a large log file).

    Note that it will project a sequence of length n into a sequence of length n-1 - you may want to prepend a "dummy" first element, for example. (Or change the method to include one.)

    Here's an example of how you'd use it:

    var query = list.SelectWithPrevious((prev, cur) =>
         new { ID = cur.ID, Date = cur.Date, DateDiff = (cur.Date - prev.Date).Days) });
    

    Note that this will include the final result of one ID with the first result of the next ID... you may wish to group your sequence by ID first.

提交回复
热议问题