c# linq xml query join

Hi

I have two xml datasets like this;

<DesItems>
    <DesItem>
      <Left>205</Left>
      <Top>99.723333333333329</Top>
      <Width>78</Width>
      <Type>Import</Type>
      <ID>48cd14ea-a3af-4a75-9271-8ff0a0a067cd</ID>
   <DesItem>
<DesItems>

AND

<Connections>
    <Connection>
      <SourceID>48cd14ea-a3af-4a75-9271-8ff0a0a067cd</SourceID>
      <EndID>667abccb-fff3-44a7-887b-0b987e83868d</EndID>
    </Connection>
</Connections>

I need to be able to get the Type from the first set of data for each of the SourceIDs and EndIDs from the bottom set using linq but am not sure how to do it.  Its like a join on id is suppose?  Sorry i'm new to linq so probably quite basic

Any advice would be great!
greepAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Does this help?
using System;
using System.Linq;
using System.Xml.Linq;

namespace _27377172
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument desItems = XDocument.Load("dsItems.xml");
            XDocument connections = XDocument.Load("dsConnections.xml");

            var query = from desItem in desItems.Descendants("DesItem")
                        join connection in connections.Descendants("Connection")
                        on desItem.Element("ID").Value equals connection.Element("SourceID").Value
                        select new
                        {
                            ItemType = desItem.Element("Type").Value,
                            SourceID = connection.Element("SourceID").Value,
                            EndID = connection.Element("EndID").Value
                        };

            foreach (var item in query)
            {
                Console.WriteLine("{0} : {1} : {2}", item.ItemType, item.SourceID, item.EndID);
            }

            Console.ReadKey();
        }
    }
}

Open in new window

käµfm³d 👽Commented:
You could also replace lines 14 - 16 with a Where:
var query = from desItem in desItems.Descendants("DesItem")
            from connection in connections.Descendants("Connection")
            where desItem.Element("ID").Value.Equals(connection.Element("SourceID").Value)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
greepAuthor Commented:
Thanks.  That is helpful but i think it only gives me the type for the source id.  I also need the type for the end id as well?
greepAuthor Commented:
Its ok. I figured it out.  Seems to work.  Much appreciated!

var query = from desItem in designerItemsXML.Descendants("DesItem")
                        from desItem2 in designerItemsXML.Descendants("DesItem")
                        from connection in connectionsXML.Descendants("Connection")
                        where desItem.Element("ID").Value.Equals(connection.Element("SourceID").Value)
                        && desItem2.Element("ID").Value.Equals(connection.Element("EndID").Value)

                        select new
                        {
                            SourceID = connection.Element("SourceID").Value,
                            SourceType = desItem.Element("Type").Value),
                            EndID = connection.Element("EndID").Value,
                            EndType = GetType(desItem2.Element("Type").Value)
                        };
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.