Filter Date in DataTable using select

Hi All,

I need your help. I have the data in excel file and uploaded into a data table (DT_ThFX). Now I have taken 2 inputs from users and storing them into 2 string variables In_StartDate and In_EndDateNow. I am getting the input from the user like 01 Jul 2021 (In_StartDate) and 30 Sep 2021 (In_EndDate)
I am trying to apply filter on my data table and want to copy to another datatable. The column name where I am applying filter is “Trade Date”. I have written the below query but I am getting the below error

DT_ThFX.Select.Where(Function(x) DateTime.Parse(x(“Trade Date”).ToString)>= DateTime.Parse( In_StartTradeDate) And DateTime.Parse(x(“Trade Date”).ToString)<= DateTime.Parse( In_EndTradeDate)).CopyToDataTable()

Filter Th FX Data Table: String was not recognized as a valid DateTime.

Can someone help me out here

Thanks in advance,


You can not add

Why are you converting it into string?

If they are the variables of date type then just keep it without .toString

Thanks for prompt reply but the data is coming from excel and store in Data table. The data in datable stored as string.

Could you please help me to get the right one?

If that’s string, and you need to perform date specific comparison then you need to convert it into date first

You can convert a string in date

Datetime.ParseExact(strDate,yyyMMdd, System.Globalization.CultureInfo.InvariantCulture)

strDate is your date in string
and yyyyMMdd you can replace as per the date in your datatable


Check this thread for resolving similar scenario

Cheers @sbniladri