Skip to content

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

Description

@CDNConsultant

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();

Activity

  1. CDNConsultant commented on Nov 21, 2025

    @CDNConsultant
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions