How to use LINQ.Where for specific indexes

As the title says i need a LINQ expression which is going to check only few indexes of array. I currently have it like this :

int[] a = b.Where(c => c % 4 == (int)Cards.CardSuits.Club).ToArray();

those are not my actual variables names i just made it a little bit shorter. I just need to check b[2] up to b[b.length-1]

Jon Skeet
people
quotationmark

If you want to use the index within your predicate instead of the value, use the overload of Where which accepts a predicate which checks a value/index pair:

int[] a = b.Where((value, index) => index % 4 == (int) Cards.CardSuits.Club)
           .ToArray();

(I'd strongly recommend you model cards differently though - consider either a struct or a class with Suit and Rank properties. Then you can check whether the suit is clubs in a much more natural way...)


If you actually wanted to keep your existing filter, but just ignore the first two elements of the source, you probably want Skip:

int[] a = b.Skip(2)
           .Where(c => c % 4 == (int) Cards.CardSuits.Club)
           .ToArray();

people

See more on this question at Stackoverflow