vb.net linq help

PhilippeRenaud
PhilippeRenaud used Ask the Experts™
on
Hello ee,

lets say I do :


                Dim q As IEnumerable(Of DataRow) = From x In dt.AsEnumerable()
                                                        Where x.Field(Of String)("Code") = _value
                                                        Select x

I have error saying invalid cast "Code"  if Int32..  I know i should change the words "Of String" and write"  Of Integer

but sometimes I will be String... becasue dt changes depending what the user do.

how can I detect wich Type is "Code" and write correctly of Field(Of 'dynamicType') ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I just tried  where x("Code") = _value


it works for interger or strings now.

but it is the correct way to do it?
Dim qry = dt.AsEnumerable().Where(Function(rw) rw("Code").ToString() = value).[Select](Function(rw) rw)

Open in new window

yes it is correct, Field and other DataTableExtensions are just type safe wrappers over DataTable. They assume that all the data in the table is of same type, if it is not then its better to directly access the value and convert it to string and do your comparison as  i did in my previous post....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial