0

I'm trying to grab some data using LINQ and a "WHERE" function within my "SELECT" function

Here's my sample XML

<Items>
<Extras>
    <Extra Code="TEST1" Quantity="1" />
    <Extra Code="TEST2" Quantity="1" />
</Extras>

<Options>
    <OptionalExtra Description="Test 1" Code="TEST1" type="TESTING1" />
    <OptionalExtra Description="Test 2" Code="TEST2" type="TESTING2"  />
</Options>
</Items>

And here's my LINQ query

bookingsInfo = xel.Descendants("Extras").Descendants("Extra") _
  .Select(Function(f) New With { _
    .Code = f.Attributes("Code").First.Value, _
    .Type = f.Parent.Parent.Descendants("Options").Descendants("OptionalExtra") _
            .Where(Function(g) g.Attributes("Code").First.Value = _
            f.Attributes("Code").First.Value).Attributes("type").First.Value, _
    .Quantity = f.Attributes("Quantity").First.Value _
})

The "TYPE" is the problem I'm having trouble getting and I'm not really sure what the issue is.

I don't have any control of how the XML is laid out but I can edit the LINQ query no problem.

Does anyone have any suggestions?

Thanks in advance

5
  • Your XML doesn't have a type attribute... so what are you trying to get? Commented Aug 6, 2014 at 13:50
  • Sorry @JonSkeet I had missed that bit out when I was typing it in! See update Commented Aug 6, 2014 at 13:52
  • Okay, so now it's there, and we know that's "the problem" - but you haven't actually described what that problem is. What is the query meant to do, and what happens at the moment? Commented Aug 6, 2014 at 13:53
  • The query is producing "CODE" and "QUANTITY" as these are just attributes but when I'm trying to go up to the parents and then into the other extras to get the "TYPE" where the "CODE" matches in each it's failing and giving me a "Sequence contains no elements" - does that make sense? Sorry for not being more descriptive in my OP Commented Aug 6, 2014 at 13:59
  • No, it doesn't really make sense. It would be much easier to follow if you'd show a short but complete program with the expected output and the actual output... Commented Aug 6, 2014 at 14:01

1 Answer 1

1

I think it will be easier to use Join instead of Parent.Parent:

Dim bookingsInfo = From e In xel.Root.<Extras>.<Extra>
                   Join o In xel.Root.<Options>.<OptionalExtra> On e.@Code Equals o.@Code
                   Select New With { 
                       .Code = e.@Code,
                       .Type = o.@Type,
                       .Quantity = e.@Quantity
                   }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - this works perfectly, I didn't realise you could use LINQ in this way it's so much easier than the way I've been taught!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.