How to limit data in datatable

I have around 15000 rows of data in my data table. I want only first 500 rows.

1)Fetch first 500 records or last 500 records
2)Split the datatable with row count as per your need

you can just ignore the rest if you cant just originally get only the first 500…

Hi @pthakre,

As you have 15000 rows in datatable already and you want only first 500, you can use take method in linq code, as given below in assign activity.

DT = DT.AsEnumerable().Take(500).CopyToDataTable()

By this it’ll take only first 500 rows.
That’s it :slight_smile:

1 Like

to be honest, if you stop and think about your code it does:

  1. Convert a data table to an ienumerable
  2. Take the first 500 rows
  3. Convert back to a data table
    this does not make sense if we already have a data table that contains the first same 500 rows… just use the original one and in the loop stop when you reach index 500…

Option 1 :
Always first 100 records
Datatable.AsEnumerable().Take(100).CopyToDataTable()

Option 2 :
Configured row count
1)Loop and move the rows to other datatable
2) Once it reaches the configured row count , Move the datatable to other table and save it in excel or you wish to process
3) Clear count and datatable

Option 2 will work for any number of rows and will you multiple tables as per the count.

1 Like

Thanks

Thanks will try :grinning:

Hi
We can go either of the options
Like if we want to get n number of variables like 100, 200 we can store that in a variable named int_rowcount of type int32

Then variable can be now used in these expressions
DT = DT.AsEnumerable().Take(int_rowcount).CopyToDataTable()

Or even in the FOR EACH ROW loop with a if condition like this
Datatable.Rows.IndexOf(row).Equals(100-1)
If true it goes to THEN part where we can use a BREAK activity

Reason for 100-1 is the row index starts from 0

Cheers @pthakre

Hi Palaniyappan,
I have written contains check and int_index is populating correct and copied to data table but I want to read Index Ex: 9 from 50 and want to skip remaining records to be copied.
I have checkpoint in file with String “End” at 50th line of my file . as per below line it is giving all lines from Index 9 to end of the excel. please let me know how can we copy from 9 to 50 in my example.
dtInput.AsEnumerable().Skip(int_index).CopyToDatatable()

Hi @Dileep,
I think by modifying
DT = DT.AsEnumerable().Take(x).CopyToDataTable() this linq to
DT = DT.AsEnumerable().Take(59).skip(9).CopyToDataTable()
we get total of 50 rows staring from 9 th row