Select a Date range using LINQ with Method Syntax

Hi All,

I need to select a Date range from 13/02/2024 to 15/05/2024. Could you please help with LINQ expression using Method syntax.

image

Thanks in advance,
Pavan.

Hi @Pavan_kumar15

Try this

Dt.AsEnumerable().Where(Function(row) DateTime.Parse(row.Field(Of String)(“OrderDate”)) >= DateTime.ParseExact(FromDate, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture) AndAlso DateTime.Parse(row.Field(Of String)(“OrderDate”)) <= DateTime.ParseExact(ToDate, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture)).CopyToDataTable()

Regards,

we highly recommend to check on how the dates are present within datatable as described below and also mentioned that it presence does not have the mandatory same format as in the excel file

A LINQ could look like this:

(From d In dtData.AsEnumerable
Let ds = d("OrderDate").toString.Trim
Let dp =DateTime.ParseExact(ds,"dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).Date
Where dp > (#02/13/2024#).Date
Where dp < (#05/15/2024#).Date
Select r=d).CopyToDataTable()

we can dynamize it by using variables instead of e.g. (#02/13/2024#).Date

Handling empty results:

1 Like

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