I am pretty new to LINQ and ran into a bit of a roadblock that I hope someone can help with. My goal is to filter out of my DT any values that are not integers in the column “Account”. This column has integers, strings and empty strings.
Here are my two attempts so far, that I can’t get to work properly:
This gets me the error of “cannot infer a common type because more than one type is possible” After researching this here, I tried using the cint function for the return, but still getting an issue.
This earlier attempt obviously crashes, as there are other datatypes in the column. Unsure if there is a way to skip rows that error. (In my dataset, it is safe to assume integers are larger than 1)
To be honest there can only be one datatype in a certain column… it will be determined by the first one as the read range will set the column datatype to be. You will need to write that IF to filter out the values that can break the filter. IsNumeric can take an object, so first thing you do is remove that .ToString from there: r(“Account”).ToString
@efleurent
The LINQ Where part is expecting a Boolean returned as the result of the expression defined for its (lets call it) function.
Lets assume you want to filter out:
column is not numeric
column is not empty or Null
so such checks has to be evaluated as usual <Evaluation Statement> Operator <Evaluation Statement> .... and then it should work
As good mentioned below it is advisable to filter out in advance rows with null values within this column can e.g. be done with filter data table or chained Where Statements
Let us know your open question. For further help maybe you can provide some sample data, that we can use for further solution development
BTW: maybe the isNumeric Check can be implement as a RegEx isMatch more clear
EDIT:- this code will work just fine unless and until it hits a blank value from an excel file…
to overcome this error
Just tweak your query a little bit by adding a .tostring instead of Cstr
Thank you @ppr@bcorrea and @vickydas for your help! Now understanding it needs a boolean as return, makes more sense on how to build the code.
The thing I am still scratching my head about, is this compiler error I am getting. I tested your xaml file and it worked no problem, went out to lunch and after re-opening and re-downloading I cant seem to get it to work again? Either way, I simply shifted the isnumeric check and working fine now. Below is the code as I used it for anyone curious.
still you should not do this part: IsNumeric(row(0).ToString) it is unnecessary and if row(0) ever contain a null value, would break your code, just dont put that ToString.