Melvin Dow
Melvin Dow

Reputation: 43

Need advice for C# LINQ List aggregation expression

Let´s say I have this List of type Prices:

[
    {
        "Id": 57571,
        "Price": 1745.0,
        "DateAdded": "2018-12-01T00:00:00"
    },
    {
        "Id": 67537,
        "Price": 1695.0,
        "DateAdded": "2018-09-24T00:00:00"
    },
    {
        "Id": 80042,
        "Price": 1645.0,
        "DateAdded": "2019-03-24T00:00:00"
    },
    {
        "Id": 155866,
        "Price": 1545.0,
        "DateAdded": "2019-04-24T00:00:00"
    },
    {
        "Id": 163643,
        "Price": 1545.0,
        "DateAdded": "2019-04-26T00:00:00"
    },
    {
        "Id": 171379,
        "Price": 1545.0,
        "DateAdded": "2019-04-27T00:00:00"
    },
    {
        "Id": 178990,
        "Price": 1545.0,
        "DateAdded": "2019-04-28T00:00:00"
    }
]

I need to remove all items with the same price, but only if the item is a sibling of the list element. Is there to achieve this by using LINQ?

My expected output would be

[
    {
        "Id": 57571,
        "Price": 1745.0,
        "DateAdded": "2018-12-01T00:00:00"
    },
    {
        "Id": 67537,
        "Price": 1695.0,
        "DateAdded": "2018-09-24T00:00:00"
    },
    {
        "Id": 80042,
        "Price": 1645.0,
        "DateAdded": "2019-03-24T00:00:00"
    },
    {
        "Id": 155866,
        "Price": 1545.0,
        "DateAdded": "2019-04-24T00:00:00"
    }
]

I have absolutely no clue how I can achieve this. I appreciate any advice.

Upvotes: 2

Views: 109

Answers (3)

Cameron MacFarland
Cameron MacFarland

Reputation: 71856

This can be done with a regular Where call.

int? previousPrice = null;

var output = myData.Where(d => {
  var result = !previousPrice.HasValue || previousPrice.Value != d.Price;
  previousPrice = d.Price;
  return result;
}).ToList();

Upvotes: 0

spender
spender

Reputation: 120380

By using the ChunkBy extension method found in Microsoft's C# documentation, you can

myData.ChunkBy(x => x.Price).Select(g => g.First())

...or by using MoreLinq's GroupAdjacent you can

myData.GroupAdjacent(x => x.Price).Select(g => g.First())

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151588

You could simply do so using an iterator:

using System;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        var input = new[] { 1, 2, 2, 3, 4, 4, 3, 2, 2, 1 };

        var output = GetUniqueAdjacent(input);

        foreach (var o in output)
        {
            Console.WriteLine(o);
        }
    }

    public static IEnumerable<int> GetUniqueAdjacent(IEnumerable<int> input)
    {
        bool first = true;
        int previous = -1;
        foreach (var i in input)
        {
            if (first)
            {
                previous = i;
                yield return i;
                first = false;
                continue;
            }

            if (i == previous)
            {
                previous = i;
                continue;
            }

            previous = i;
            yield return i;
        }
    }
}

This outputs 1, 2, 3, 4, 3, 2, 1, removing duplicates only if they're adjacent.

Upvotes: 2

Related Questions