LINQ: Join Between a Collection and XML Data

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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by srinivas on Tuesday, June 3, 2014 6:52 AM
awesome.................