Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to display selected columns from datatable using LINQ

Posted on 2010-09-09
3
Medium Priority
?
646 Views
Last Modified: 2013-12-17
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?

0
Comment
Question by:kravindra
  • 2
3 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 33640575
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
0
 

Author Comment

by:kravindra
ID: 33640712
We are using Oracle database. the type FUNDS_RECEIPT_BATCH_ID is NUMBER.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 33640962
The datatype NUMBER in Oracle gets translated to Decimal in .Net. See the .Net documentation for Datatype mapping.

Oracle Data Type Mappings
http://msdn.microsoft.com/en-us/library/yk72thhd.aspx
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question