LINQ: Join Between a Collection and XML Data

Posted by: Pravinkumar Dabade , on 5/30/2011, in Category LINQ
Views: 50562
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 –

LINQ Customer XML

Now let’s add two classes in our project with the name ‘Customers’ and ‘CustomerOrders’ as shown below –

Customer Class

Add a method which will load an XML file and query the XML file as shown below –

Load XML file

Now let’s access this XML file using LINQ query. Write the following code in our Main method as shown below –

image

Now when you run the project, you will see the following output –

LINQ Query Collection XML

Now let’s add a method which will return orders related to our customers as shown below –

LINQ Query Collection XML

Now let’s write a LINQ query to fetch the orders data in our Main method as shown below –

LINQ Query

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 –

LINQ Query Collection XML

To print the values of the join statement, write the following code –

LINQ Query Collection XML

Now when you run this program you will see the below output –

LINQ Query Collection XML

The entire source code of this article can be downloaded over here

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar[attherate]gmail[dot]com


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by srinivas on Tuesday, June 3, 2014 6:52 AM
awesome.................

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel