Avatar of kravindra
kravindra
 asked on

How to display selected columns from datatable using LINQ

I am passing datatable instance to the dataadapter to fetch the resultset from database.the datatable default it doesn't any schema.

Datatable dt = new DataTable();

datadapter da = new dataadapter();

da.fill(dt);

               var fundsReceiptsbatchids = (from batchids in dt.AsEnumerable()
                                            select new
                                            {
                                                FUNDS_RECEIPT_BATCH_ID =
                                                    batchids.Field<int>("FUNDS_RECEIPT_BATCH_ID")
                                            }).ToList();

when i try to excute this LINQ query, I am getting an expection like
Specified cast is not valid.

I have question in  LINQ query  in select block I am assigning datatable column value
(i.e batchids.Field<int>("FUNDS_RECEIPT_BATCH_ID"))  to this field FUNDS_RECEIPT_BATCH_ID. i didn't mention this FUNDS_RECEIPT_BATCH_ID field in my class or anywhere.

Actually FUNDS_RECEIPT_BATCH_ID is a one of the column in my resultset, which i am fetching from database using dataadapter.

How to get the selected columns from datatable using LINQ?

.NET Programming

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

Hi kravindra;

To your question, "I have question in  LINQ query  in select block I am assigning datatable column value
(i.e batchids.Field<int>("FUNDS_RECEIPT_BATCH_ID"))  to this field FUNDS_RECEIPT_BATCH_ID. i didn't mention this FUNDS_RECEIPT_BATCH_ID field in my class or anywhere. Actually FUNDS_RECEIPT_BATCH_ID is a one of the column in my resultset, which i am fetching from database using dataadapter.", the runtime will type it so this is not an issue.

Make sure that the field FUNDS_RECEIPT_BATCH_ID in the datatbase is truely an int and not something else.

Fernando
kravindra

ASKER
We are using Oracle database. the type FUNDS_RECEIPT_BATCH_ID is NUMBER.
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61