Linq to join tables on XElement.Name = XAttribute(“n”)?

Some of your inputs aren't aligned quite right. Here's a good starting place: var rowText = @" ABC123 10 123 Main St. , NW Anytown etc. "; var sourceEntriesText = @" etc."; var templateText = @" etc. "; var clientXm = XDocument. Parse(rowText); var sourceEntries = XDocument.

Parse(sourceEntriesText); var results = from r in clientXm. Descendants("row") from t1 in r.Elements() let xRef = sourceEntries. Descendants("Field").

FirstOrDefault( t2 => t1. Name == t2. Attribute("n").

Value) select new { t1, xRef } A little more work yields something probably closer to what you're looking for: var results = from r in clientXm. Descendants("row") from t1 in r.Elements() join t2 in sourceEntries. Descendants("Field") on t1.Name equals t2.

Attribute("n"). Value join t3 in template. Descendants("Field") on t2.

Attribute("m"). Value equals t3. Attribute("id").

Value select new { Name = t3. Attribute("n"). Value, Value = t1.

Value, }.

Some of your inputs aren't aligned quite right. Here's a good starting place: var rowText = @" ABC123 10 123 Main St. , NW Anytown etc. "; var sourceEntriesText = @" etc."; var templateText = @" etc. "; var clientXm = XDocument. Parse(rowText); var sourceEntries = XDocument.

Parse(sourceEntriesText); var results = from r in clientXm. Descendants("row") from t1 in r.Elements() let xRef = sourceEntries. Descendants("Field").

FirstOrDefault( t2 => t1. Name == t2. Attribute("n").

Value) select new { t1, xRef }; A little more work yields something probably closer to what you're looking for: var results = from r in clientXm. Descendants("row") from t1 in r.Elements() join t2 in sourceEntries. Descendants("Field") on t1.Name equals t2.

Attribute("n"). Value join t3 in template. Descendants("Field") on t2.

Attribute("m"). Value equals t3. Attribute("id").

Value select new { Name = t3. Attribute("n"). Value, Value = t1.

Value, }.

This looks like it should work; particularly the second example. Unfortunately, I'm not getting any results. Gonna have to start with little baby steps and see how far I get.

Thank you, though, for the help. – EoRaptor013 Dec 15 '10 at 14:47 @EoRaptor013: I used the given code in LINQPad, and it produced results. You might want to check your XML inputs.

Your producer code mapping, for example, doesn't match. There were a few other issues, too. – StriplingWarrior Dec 15 '10 at 16:07 No joy.

Where this seems to blow up is that I can't get a match between the client data XElement.XName. LocalName and the map data (string) XElement. XAttribute("n") name.

Even though the two strings actually do match -- from looking at them in the source files, the join can't pick it up. Is there a way to debug these statements so I can see what attribute n is returning? Thanks.

– EoRaptor013 Dec 15 '10 at 19:34 Downloaded LinqPad (as a possible debugger). A simple query returned data. That selfsame query in VS returns no results.Is there any way to trace through the execution of the statements in VS?

– EoRaptor013 Dec 15 '10 at 20:18 @EoRaptor013: Not really. That's one of the weaker points of LINQ. However, you should get the same results from LINQPad as you get in Visual Studio.

I strongly suspect that either your code or your XML is different. Try pasting everything from LINQPad directly into Visual Studio and see if you get results then. – StriplingWarrior Dec 15 '10 at 20:30.

Well it's easy to fix the compiler errors: var results = from t1 in clientXm. Elements("row").Descendants() join t2 in sourceEntries. Elements("Field") on t1.Name.

LocalName equals (string) t2. Attribute("n") into xRef select new { N = (string)xRef. Attributes("n"), M = (string)xRef.

Attributes("m") }; However, I'm not sure how you're intending the cast from IEnumerable to string to work in the projection... I suspect you don't want a group join (which is what you get with join...into. But I don't understand how your data sample works, so it's hard to give you the correct code. Where should ClientId come from, for example?

You've only got it in your sample output... assuming that is your sample output, of course. Basically, if you could simplify your sample data into two inputs and an output, and explain how the output is derived, that would make life easier... at the moment it's not clear.

CustomerID in client file matches to SourceEntries attribute n='CustomerID'. SourceEntries attribute m='201' then points to template attribute id='201'. Template attribute n='ClientId' gives me the new element name.

– EoRaptor013 Dec 14 '10 at 19:00 So far, all I'm trying to do here is simply retrieve the appropriate 'n' and 'm' attributes from the Source Entries file. The next step would be to join the Template file on sourceEntries. M = template.id.

I would then create a new XML tree using the template attribute n value as the element name. If there's a better way to do this, given the constraints in the three files, I would be terribly grateful to hear it. – EoRaptor013 Dec 14 '10 at 19:04.

So, from the client file XElement name, match the Source Entries attribute value 'n', and get the Source Entires map reference m. Then, use m to match a template element attribute i, retrieve the template name attribute 'n' and create a new XElement tree using that name. Since I'm a noob at Linq, and not particularly adept at XML, I thought I'd start out slowly.

First error, in the join clause at "on t1.Name. LocalName equals" is that Argument type 'string' is not assignable to parameter type 'TKey'. Then, in the select clause, for the two items I'm interested in, I get "Anonymous type projection initializer should be simple name or member access expression.".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions