Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Selecting columns from two tables using linq

Posted on 2013-07-01
6
Medium Priority
?
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 64

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 64

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 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