Solved

How to display selected columns from datatable using LINQ

Posted on 2010-09-09
3
633 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 62

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 62

Accepted Solution

by:
Fernando Soto earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now