Solved

Selecting columns from two tables using linq

Posted on 2013-07-01
6
755 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

12 Experts available now in Live!

Get 1:1 Help Now