Solved

Selecting columns from two tables using linq

Posted on 2013-07-01
6
762 Views
Last Modified: 2013-07-02
I am trying to do a join on two dattables and select columns from the two tables

 var result = from dataRows1 in infocodesDT.AsEnumerable()
                          join dataRows2 in mqDT.AsEnumerable()
                          on dataRows1["Code"] equals dataRows2["Code"]
                          select new DataRow  { dataRows1["Code"] ,dataRows1["Desc"],  dataRows2["BusDate"] , dataRows2["Price"]};

Gives me the foll error

.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level
0
Comment
Question by:countrymeister
  • 3
  • 2
6 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39289798
Hi  countrymeister;
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39289800
you can't create dynamic DataRow cause it bound to schema.
here u try to create new schema joined from two tables.
so what u want to do is this:

var result = from dataRows1 in amenity.AsEnumerable()
                         join dataRows2 in hotelDetails.AsEnumerable()
                         on dataRows1["Code"] equals dataRows2["Code"]
                         select new { code = dataRows1["Code"], desc = dataRows1["Desc"], busDate = dataRows2["BusDate"], price = dataRows2["Price"] };

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39289806
Hi  countrymeister;

According to Microsoft documentation DataRoad and DataRowBuilder are for internal use only. What you may want to do is to extract the data from each row and then build a new table with the results.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39289811
this code create the joined table first, then use NewRow.ItemArray to file the properties from both tables:
            DataTable joinedTable = new DataTable();
            joinedTable.Columns.Add("Code");
            joinedTable.Columns.Add("Desc");
            joinedTable.Columns.Add("BusDate");
            joinedTable.Columns.Add("Price");
            
            var result = from dataRows1 in amenity.AsEnumerable()
                         join dataRows2 in hotelDetails.AsEnumerable()
                         on dataRows1["Code"] equals dataRows2["Code"]
                         select joinedTable.NewRow().ItemArray = new object[]{
                             dataRows1["Code"],
                             dataRows1["Desc"],
                             dataRows2["BusDate"], 
                             dataRows2["Price"] };

Open in new window

0
 
LVL 1

Author Comment

by:countrymeister
ID: 39289909
sedqwick

I need to get the var result into a datatable, so int the second example, will the result populate the datatable.

Your first example works but I need the var result to a datatable
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39289963
here:
            DataTable joinedTable = new DataTable();
            joinedTable.Columns.Add("Code");
            joinedTable.Columns.Add("Desc");
            joinedTable.Columns.Add("BusDate");
            joinedTable.Columns.Add("Price");

            var result = from dataRows1 in amenity.AsEnumerable()
                         join dataRows2 in hotelDetails.AsEnumerable()
                         on dataRows1["Code"] equals dataRows2["Code"]
                         select joinedTable.NewRow().ItemArray = new object[]{
                             dataRows1["Code"],
                             dataRows1["Desc"],
                             dataRows2["BusDate"], 
                             dataRows2["Price"] };
            foreach (var row in result)
            {
                joinedTable.Rows.Add(row);
            }

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

16 Experts available now in Live!

Get 1:1 Help Now