System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Issue with Where Clause for a DataTable - String Works - Integer does not... ?

Open CDNConsultant opened this issue 2 months ago • 1 comments

Hello,

I have a bunch of DataTables I need to query dynamically.
I have a GenerateClause function that generates a simple where clause based on the type of column being queried...

For example, a String column might look like (City = "Seattle") and an Int32 field might look like (Size = 3)

However, the string field where clause works fine - but the Int32 field does not work - and generates an error "Specified Cast is not Valid"

Any thoughts as to what might be causing this error message? I will need to put DataTime, Doubles, DateOnly, etc. where clauses as well as the String and Int32.

Here is the code I am using - where the GenateWhereClause is used to gen the string containing the actual clause...

                DataTable ColumnSelTable = dataSet_DataCleaning.Tables["RemoveByCol"];
                CurrWhereClause = GenerateWhereClause(ColumnSelTable);

                var dynamicQuery = CurrTable.AsEnumerable().AsQueryable();
                var dynamicQuery2 = dynamicQuery.Where(DynLinqConfig, CurrWhereClause).ToList();
                ResultingDataTable = dynamicQuery2.CopyToDataTable();

CDNConsultant avatar Nov 19 '25 05:11 CDNConsultant

It appears that using DynamicLINQ with Int64, Double, and some other field types all hinges on how you have the Where clause formatted

I was using a where clause that was generated and it looked like Int64Col == 11 for example - and it failed when I then ran the following command dynamicQuery.Where(DynLinqConfig, CurrWhereClause).ToList();

HOWEVER, if you use parameters such as ...

Int64 TempI64 = 11;
dynamicQuery.Where(DynLinqConfig, "Int64Col == ", TempI64).ToList();

It all works fine. It is functionally the same - but DynamicLINQ seems to have an issue telling how to map the datatype if you don't use parameters.
This will make it a little difficult to use my current GenerateWhereClause code - but I can figure a way around that.

Thought I would post this here to help anyone else that runs into this issue.

CDNConsultant avatar Nov 21 '25 23:11 CDNConsultant