473,379 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How To Join Multiple Datatables Using LINQ To Dataset

Hi,

I have been searching for quite a while about joining multiple datatables via LINQ to dataset (When i say multiple, i don't mean just 2!!!), and it seems there aren't lot of examples in the net. I have seen examples of joining two datatables, done that, no problem there. Now i want to join three datatables. And there's nothing i can find out there.

For example i have a 3 datatables:

PERSON (columns: person_id, name, gender_code, ethnic_code)
GENDER (columns: gender_code, gender_description)
ETHNICITY (cols: ethnic_code, ethnic_description)

with sample records of:

PERSON:
p001, John B. Jones, m, 1
p002, Misa L. Campo, f, 3

GENDER:
m, Male
f, Female

ETHNICITY:
1, White American
2, African American
3, Asian

I'm hoping to display something like this:
p001, John B. Jones, Male, White American
p002, Misa L. Campo, Female, Asian

I have the ff codes:

Expand|Select|Wrap|Line Numbers
  1.  Dim joinedResult = _
  2.             From p In PERSON.AsEnumerable() _
  3.             Join g In GENDER.AsEnumerable() _
  4.             On g.Field(Of string)("gender_code") Equals p.Field(Of string)("gender_code") _
  5.             Join e In ETHNICITY.AsEnumerable() _
  6.             On e.Field(Of STRING)("ethnic_code") Equals p.Field(Of string)("ethnic_code") _
  7.             Select _
  8.             id = p.Field(Of String)("person_id"), _
  9.             name = p.Field(Of String)("name"), _
  10.             gender = g.Field(Of String)("gender_description"), _
  11.             ethnicity = e.Field(Of String)("ethnic_description")
  12.  
I believe i'm doing this right up to this point, now the problem is i cant use the CopyToDataTable method on joinedResult. Any help would be greatly appreciated.

Thanks guys,
Feb 8 '12 #1

✓ answered by chibbie23

It seems that when i try to explicitly select the fields i want, it returns an iEnumerable(of anonymous) type and CopyToDataTable method expects an iEnumerable(of Datarow) type. So the workaround is to create a datatable with columns representing the fields you want to select and then loop through the rows in the results and then add the records one at a time.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim dt as new datatable
  3. dt.columns.add("ID",gettype(string))
  4. dt.columns.add("Name",gettype(string))
  5. dt.columns.add("Gender",gettype(string))
  6. dt.columns.add("Ethnicity",gettype(string))
  7.  
  8. For Each row in joinedResults
  9.     Dim dr As DataRow = dt.NewRow()
  10.     dr("ID") = row.ID
  11.     dr("Name") = row.Name
  12.     dr("Gender") = row.Gender
  13.     dr("Ethnicity") = row.Ethnicity
  14.     dt.Rows.Add(dr)
  15. Next
  16.  
  17.  
I just couldn't verify if this is the only way around it. :D

1 15803
It seems that when i try to explicitly select the fields i want, it returns an iEnumerable(of anonymous) type and CopyToDataTable method expects an iEnumerable(of Datarow) type. So the workaround is to create a datatable with columns representing the fields you want to select and then loop through the rows in the results and then add the records one at a time.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim dt as new datatable
  3. dt.columns.add("ID",gettype(string))
  4. dt.columns.add("Name",gettype(string))
  5. dt.columns.add("Gender",gettype(string))
  6. dt.columns.add("Ethnicity",gettype(string))
  7.  
  8. For Each row in joinedResults
  9.     Dim dr As DataRow = dt.NewRow()
  10.     dr("ID") = row.ID
  11.     dr("Name") = row.Name
  12.     dr("Gender") = row.Gender
  13.     dr("Ethnicity") = row.Ethnicity
  14.     dt.Rows.Add(dr)
  15. Next
  16.  
  17.  
I just couldn't verify if this is the only way around it. :D
Feb 15 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Craig G | last post by:
i was told that its possible to load more than 1 datatable into a dataset using a stored procedure i need to fill 3 combo's on my form if i had a SQL Stored Proc that had 3 different select...
2
by: Bjorn | last post by:
Hi My name is Bjorn and I have a VB question: How do I join multiple rows in VB? Example I have the data in a format like this: result 1 result 2 11104 8
3
by: Joe Kovac | last post by:
Hi! I have got a very complex database schema. For example a customer is constructed within a Customer table and a Person table. I have both tables as DataTables in a DataSet. When I want to...
1
by: james | last post by:
Hi Guys, Is it possible to group by multiple Fields in LINQ? I want to do something similiar to SQL command: SELECT Count(*) FROM MyTable GROUP BY Field1, Field2 Thanks!
2
by: kjewell23 | last post by:
Hello. I'm trying to inner join two tables into one dataset. Is that possible. If so I do I do it. I have my code down below, so if you could help me. please!! I'm using asp.net for my coding Dim...
4
by: Lacutas | last post by:
Hi all, I am having some problems using LINQ to access Distinct records from a Dataset. I have looked around and believe it should be as simple as added .Distinct() to my LINQ query below, though...
0
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
2
by: Arjen | last post by:
Hi, How can I delete multiple records with Linq? On the internet I found the DeleteAll function, but this one does not exist in my library. Please help me out. Thanks!
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.