LINQ: Join Between a Collection and XML Data
Posted by: Pravinkumar Dabade
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
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 pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!