leigero leigero - 7 days ago 6
C# Question

How do you remove trailing objects from a list with linq?

I have a collection of objects with properties and I want to remove all the trailing objects with (say) a value of

0
in LINQ.

public class Object
{
public Object(){}

public int Property {get; set;}
}


and if I have a list of objects:

new Object(){ Property = 1};
new Object(){ Property = 0};
new Object(){ Property = 9};
new Object(){ Property = 7};
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"


How would I go about removing the "trailing zeros" in this list? I don't want to remove all properties with a zero, but I want to remove any objects from the list with a property value of zero if it it is not later followed by a property value of something greater.

Answer

Standard solution for sequences of finite size - reverse, remove from start, reverse:

   var withoutTail = sequence
       .Reverse()
       .SkipWhile( x => x == 0) // whatever condition you need
       .Reverse();

This is very non-optimal, so if you actually have real collection (i.e. List) it would be better to just remove items starting from last index.

Comments