Gohyu
Gohyu

Reputation: 478

C# - Linq statement where skip last X rows

I have a dynamic table where show/hide rows.

In this variable I skip first row, but I want skip last n rows...

var allVisibleRows1 = myTbl.Rows.Cast<TableRow>().Where(row => row.Visible).Skip(1);

(skip 1) 2 3 4 5 (skip 6) (skip 7)

How to do this?

Upvotes: 2

Views: 1659

Answers (2)

Piyg
Piyg

Reputation: 244

Could you try this:

var allVisibleRows1 = myTbl.Rows.Cast<TableRow>().Where(row => row.Visible);
LastNRowsSkipped = allVisibleRows1.Take(allVisibleRows1.Count() - N);

The only caveat with this is, we are enumerating over the collection twice -- Once for the count and once for filtering.

Upvotes: 3

Hari Prasad
Hari Prasad

Reputation: 16966

You could use Take extension method and specify required count.

var allVisibleRows1 =myTbl.Rows.Cast<TableRow>()
                               .Where(row => row.Visible)
                               .Skip(1)
                               .Take(4); // specify count. Apply Math(if requied).

Upvotes: 8

Related Questions