Reputation: 7005
I have a DataTable:
Position Price
1 1.2
1 1.3
2 1.25
1 1.4
1 1.3
2 1.35
I want to return all rows where Position has changed:
Position Price
2 1.25
1 1.4
2 1.35
Is there an elegant way to do this with Linq?
I tried:
var trades = Enumerable.Range(1, Data.Rows.Count - 1)
.Where(i => !Data.Rows[i].Field<string>("Position").Equals(Data.Rows[i - 1].Field<string>("Position")));
but this returns a list of indices, not the rows.
Upvotes: 2
Views: 65
Reputation: 460238
With LINQ:
var positionChanges = table.AsEnumerable()
.Skip(1)
.Where((row, index) => table.Rows[index - 1].Field<string>(0) != row.Field<string>(0));
Upvotes: 4