54 69 6d
54 69 6d

Reputation: 165

linq query getting Unable to cast object of type 'System.Boolean' to type 'System.String'

My problem is in the Where clause table1.Field<string>(3) == table2.Field<string>(3). These values are either a 0 or 1, which i think linq changes it to a bool value. Linq gets "Unable to cast object of type 'System.Boolean' to type 'System.String'".

I tried table1[3].ToString() == table2[3].ToString(), but it comes up with no errors or matching rows, which I know it should come up with some.

My query:

var match = (from table1 in dt1.AsEnumerable()
                     join table2 in dt2.AsEnumerable() on table1[1].ToString() equals table2[1].ToString())
                     where table1.Field<string>(3) == table2.Field<string>(3)
                     && table1.Field<string>("ID") == table2.Field<string>("ID")
                     select table1).ToList();

I'm looking to add more where clauses on the row, but it fails on the ones that have a 0 or 1 value to compare.

Thanks for the help

Upvotes: 1

Views: 2103

Answers (1)

NetMage
NetMage

Reputation: 26917

So it looks like the root problem is table1[3] is System.String and table2[3] is System.Boolean. So the cast is failing on table2.Field<string>(3).

So replace your where with the appropriate test:

where (table1.Field<string>(3) == "1") == table2.Field<bool>(3)

Upvotes: 2

Related Questions