Filter Datatable but trying C#

Hi all

This isn’t really a Ui path question at this stage - but likely to become one if this question progresses - I’m trying to learn C# to supplement my Ui path skills. So in my current project, I would scrape some info off the screen and use filter datatable to get what I need. For the time being I am trying to create the table in C# and then get the info.

My code

produces this table


The line I need to pull out is the one which contains a receipt number beginning with E but of the latest date so it would be the 4th entry of 5 in the table. I’m struggling to pull out this data and am after help, please. My thoughts are, would I need to sort the data table in to date order guaranteeing that I would get the latest date, then use a for each loop to get the first value containing a receipt number beginning with E? Or are there other way?

Out of interest if you are able to help, do you know why, when I run the code in visual studio the datatable doesn’t show the headers? and I see there is loads of (conflicting) info on the date format. I’d like to get rid of the time if that’s possible?

Thanks all

You could use Select() to filter out the rows with receipt number starting with E and then sort those rows in descending order. The first one would be the latest date:

var matchedRow = Receipts.Select("[Receipt Data] LIKE 'Receipt number is E*'", "Date DESC").FirstOrDefault();
if (matchedRow != null)
	Console.WriteLine(String.Join(" | ", matchedRow.ItemArray)); // print the entire row
	Console.WriteLine("No match found!");

The headers are stored in Receipts.Columns. Loop through them and print x.ColumnName.

Unfortunately, there’s no Date only type so you have to deal with DateTime (unless you want to create your own class or using a third party library). You could e.g. check for the type DateTime and then format it accordingly using ToString().

if (item.GetType() == typeof(DateTime))
1 Like

Thank you so much for replying. This is really helpful :slight_smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.