How to group the same values in a sequence with LINQ?

.net, c#, linq

Solution

var list = new List<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
        List<int> result = list.Where((x, index) =>
        {
            return index == 0 || x != list.ElementAt(index - 1) ? true : false;
        }).ToList();

This returns what you want. Hope it helped.

Problem

I have a sequence. For example: ``` new [] { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 } ``` Now I have to remove duplicated values without changing the overall order. For the sequence above: ``` new [] { 10, 1, 5, 25, 45, 40, 100, 1, 2, 3 } ``` How to do this with LINQ?

Original source