I have a very large excel sheet with tens of thousands of rows. Column A contains dates in the format dd/mm/yyyy. I want to filter out the rows that have todays date -1 day. While this is simple to do it takes a long time for the ‘read range’ activity to read through the whole excel sheet and then to do a ‘for each row’.
Is there any other ways of doing this that would be quicker?
First step about generating dates is just for demo purpose. You will use your datatable e.g. from read range Excel.
filtering the datatable With the help of LINQ - returning the filtered rows as a list
If result is not empty then copytodatatable else create empty datatble by cloning origin datatable
LINQ Statement:
(From d In dtData.AsEnumerable
iterate over all rows, reference looped row with d
Where now.AddDays(-1).Date = dateTime.ParseExact(d(0).toString,“dd/MM/yyyy”,
CultureInfo.InvariantCulture).Date
evalutate if the parsed date from current row is equal to yesterdays date
Select d).toList
if the row passed the filter then add it to a list
@Automater999
Yes empty string values are not a valid datetime string. We can handle (block it) with following statement
(From d In dtData.AsEnumerable
Where Not If(IsNothing(d(0)), True, String.IsNullOrEmpty(d(0).toString.Trim))
Where now.AddDays(-1).Date = dateTime.ParseExact(d(0).toString,"dd/MM/yyyy", CultureInfo.InvariantCulture).Date
Select d).toList
preserve format / not preserve format should not ommit rows. However give a try and verify the result within a debug / breakpoint run. Maybe the date pattern will be changed and requires an adoption. But you will see it within the debugging
As mentioned above when changing the preserve format option the datetime parse pattern is to check and maybe to adopt. Just check it while debugging and inspecting the datatable content
@Automater999
in the LINQ statement every place where d(0) is used the column is referenced with the index and does mean that the first column is used. As common used it can be referenced by name or column index.
Just check the excel on date strings that will not fit to the defined format pattern