Linq query(2)

I have 4 columns in a excel sheet, in that one column is of date, so i want to get the whole row of data from excel using just date column of some specified range?

1 Like

Hope this would help you

Cheers @Manish540

Can you explain this code to me.
dataTable.Select(“date >= #” + 15/09/2019 + “# AND date <= #” + 18/09/2019 + “#”).CopyToDataTable

what does ‘#’ and ‘date’ represent in the code?

‘#’ is for the syntax using date datatype as a filter.
date is the column name of your datatable.

1 Like

It’s giving me error as, disallows implicit conversions from from ‘string’ to ‘double’.

Hello @Manish540
Try this
DT.AsEnumerable().Where(Function(r) cdate(r("ColumnName"))< cdate("YourDate") or CDate(r("ColumnName"))> cdate("Yourdate") ).CopyToDataTable

2 Likes

19.4.4+Branch.support/v2019.4.Sha.6d308c7f5a32b559373887ae421ce7eaec8f8d24

Source: Assign

Message: Conversion from string “15/09/2019” to type ‘Date’ is not valid.

Exception Type: System.InvalidCastException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidCastException: Conversion from string “15/09/2019” to type ‘Date’ is not valid.
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
at lambda_method(Closure , DataRow )
at System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext() at System.Data.DataTableExtensions.LoadTableFromEnumerable[T](IEnumerable1 source, DataTable table, Nullable1 options, FillErrorEventHandler errorHandler) at System.Data.DataTableExtensions.CopyToDataTable[T](IEnumerable1 source)
at lambda_method(Closure , ActivityContext )
at Microsoft.VisualBasic.Activities.VisualBasicValue1.Execute(CodeActivityContext context) at System.Activities.CodeActivity1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity1 expressionActivity) at System.Activities.InArgument1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate) at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Getting this error.

My code,
dataTable.AsEnumerable().Where(Function(r) cdate(r(“Estimated Date Of Arrival”))< “15/09/2019” or CDate(r(“Estimated Date Of Arrival”))>“18/09/2019” ).CopyToDataTable

hello @Manish540

Hi Vishal whether the above code is working for u?

Hello @Manish540
Please copy the code as it is.you have skipped cdate in your code after mathematical signs
and one more thing put your date in MM/dd/yyyy format

yes bro

One more question, how to filter date column, just by giving the month and the year?

Hello @Manish540

you can do that by using this code

DT.AsEnumerable().Where(Function(r) cdate(r("C1")).ToString("MM-yyyy") = ("09-2019")).CopyToDataTable

and also you can learn basics of Linq using this site or by doing quick web search so that you’ll never face problems like this.
Check it out.

LINQ samples in LINQ to Objects

1 Like

Thank You

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