Solved

Selecting columns from two tables using linq

Posted on 2013-07-01
6
769 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
DevOps Toolchain Recommendations

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

 
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

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 article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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