Mahmoud A. Alyan
Mahmoud A. Alyan

Reputation: 11

How to start counting a specific List elements based on a given index, and a condition?

var traps = new List<trap>();
if(traps.Count(x => x.trapToughness == tough.weak) => 5)
{
    //Some Code
}

I don't want the count method to start counting from the beginning of the List.

I have a local variable which holds the index(0,5,10,...)

Upvotes: 0

Views: 89

Answers (2)

Gilad Green
Gilad Green

Reputation: 37299

var traps = new List<trap>();
var counter = traps.Skip(indexVariableToStartFrom)
                   .Count(x => x.trapToughness == tough.weak);

If(counter >= 5) 
{
    // Some code
}

Upvotes: 1

Zeph
Zeph

Reputation: 1728

The Where clause includes an index as the second parameter. This is the index of the item in the IEnumerable

var traps = new List<trap>();
if(traps.Where((x, y) => x.trapToughness == tough.weak && y > INDEX)
                          .Count() >= 5)
{
    //Some Code
}

Upvotes: 0

Related Questions