Andris Mudiayi
Andris Mudiayi

Reputation: 469

c# how to trim null elements at the start and end of a list

given a list of objects whose values are for

StatusList = new List<StatusData>
                {
                    new StatusData {Count = 0, Id = 1},
                    new StatusData {Count = 0, Id = 2},
                    new StatusData {Count = 1, Id = 3},
                    new StatusData {Count = 0, Id = 4},
                    new StatusData {Count = 2, Id = 5},
                    new StatusData {Count = 3, Id = 6},
                    new StatusData {Count = 0, Id = 7},
                    new StatusData {Count = 0, Id = 8},
                    new StatusData {Count = 0, Id = 2}
                };

how do i trim the left side and right side of the list by removing the elements with zeros?

Upvotes: 1

Views: 341

Answers (5)

Sam Arustamyan
Sam Arustamyan

Reputation: 528

Not quite efficient but more readable solution would be:

StatusList.Reverse();
StatusList = StatusList.SkipWhile(x => x.Count == 0).ToList();
StatusList.Reverse();
StatusList = StatusList.SkipWhile(x => x.Count == 0).ToList();

Upvotes: 1

Andris Mudiayi
Andris Mudiayi

Reputation: 469

This is how i resolved it...

RemoveOutsideZeros(ref StatusList);
                StatusList.Reverse();
RemoveOutsideZeros(ref StatusList);


private void RemoveOutsideZeros(ref List<StatusData> StatusList)
{
    bool Found0 = false;
    foreach (StatusData i in StatusList.ToList())
    {
        if (i.Count != 0)
    {
        Found0 = true;
    }
        if (i.Count == 0 && Found0 == false)
        {
            StatusList.Remove(i);
        }
    }

}

Upvotes: 0

Ralf de Kleine
Ralf de Kleine

Reputation: 11744

// Remove until count != 0 is found
foreach (var item in StatusList.ToArray())
{
    if (item.Count == 0)
        StatusList.Remove(item);
    else
        break;
}
// Reverse the list
StatusList.Reverse(0, StatusList.Count);
// Remove until count != 0 is found
foreach (var item in StatusList.ToArray())
{
    if (item.Count == 0)
        StatusList.Remove(item);
    else
        break;
}
// reverse back
StatusList.Reverse(0, StatusList.Count);

Upvotes: 2

Oded
Oded

Reputation: 499132

Using LINQ:

var nonEmptyItems = StatusList.Where(sd => sd.Count > 0);

nonEmptyItems will contain the items with Count greater than 0, including the middle one.

Alternatively, if you don't want that central item removed, you can use a while loop and remove each empty item from the front and back till non such items exist.

var trimmed = false;
while(!trimmed)
{
  if(StatusList[0].Count == 0)
     StatusList.RemoveAt(0);

  if(StatusList[StatusList.Count - 1].Count == 0)
    StatusList.RemoveAt(StatusList.Count - 1);

  if(StatusList[0].Count == 0 > StatusList[StatusList.Count - 1].Count > 0)
    trimmed = true;
}

Upvotes: 2

Marek Musielak
Marek Musielak

Reputation: 27132

int start = 0, end = StatusList.Count - 1;

while (start < end && StatusList[start].Count == 0) start++;
while (end >= start && StatusList[end].Count == 0) end--;

return StatusList.Skip(start).Take(end - start + 1);

Upvotes: 4

Related Questions