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?
 
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

0
 
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

0
 
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?
0
 
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)
                        };
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.