Solved

How to display selected columns from datatable using LINQ

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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