Solved

Selecting columns from two tables using linq

Posted on 2013-07-01
6
785 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 63

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 63

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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