LINQ: Join Between a Collection and XML Data
Posted by: Pravinkumar Dabade ,
on 5/30/2011,
in
Category LINQ
Abstract: In this article, we will see how to perform a JOIN between a Collection and XML Data using LINQ.
In this article, we will see how to perform a JOIN between a Collection and XML using LINQ.
If you are new to LINQ, I recommend you read LINQ to XML Tutorials here and some General LINQ articles over here to get up and running with LINQ.
Let's first create a simple console based application and a XML file with the name ‘Customers.xml’ as shown below –
Now let’s add two classes in our project with the name ‘Customers’ and ‘CustomerOrders’ as shown below –

Add a method which will load an XML file and query the XML file as shown below –
Now let’s access this XML file using LINQ query. Write the following code in our Main method as shown below –
Now when you run the project, you will see the following output –
Now let’s add a method which will return orders related to our customers as shown below –
Now let’s write a LINQ query to fetch the orders data in our Main method as shown below –
Run the application and you will get the result of all the orders. Now let’s write a JOIN query which will fetch customer data along with their orders in the Main method as shown below –
To print the values of the join statement, write the following code –
Now when you run this program you will see the below output –
The entire source code of this article can be downloaded over here
Give me a +1 if you think it was a good article. Thanks!