linq compare with previous item

linq

Solution

I think this is what are you looking for:

test.Where((x,idx) => idx == 0 || x.Country != test[idx - 1].Country));

Problem

I need to filter a list, by removing all items have the same language code as the item before (the items are orderd by time). This way, I want to detect all border crossings. Is there a way to do this with one line of linq? ``` var test = new List<Sample> { new Sample("AT", "test1"), new Sample("AT", "test2") , new Sample("AT", "test3") , new Sample("DE", "test4") , new Sample("DE", "test5") , new Sample("DE", "test6") , new Sample("AT", "test7") , new Sample("AT", "test8") , new Sample("AT", "test9") }; var borderChanges = new List<Sample>(); var lastCountry = ""; foreach (var sample in test) { if (sample.country != lastCountry) { lastCountry = sample.country; borderChanges.Add(sample); } } ```

Original source