Can someone shed some light on thsi query?
-
Tuesday, March 05, 2013 6:16 PM
Hi All,
I have a query like such,
var chargeItemListsTest = from cis in productLine.Elements("ChargeItemList") select new { ServiceNo = (string)cis.Element("ServiceNo"), SOs = from s in cis.Elements("ChargeItem").Where(c => c.Attribute("SortKey").Value == "SomeCategory") select s.Elements("SupplementaryOffer") };My XML looks something like,
<ChargeItemList> <ServiceNo>A Number</ServiceNo> <PrimaryOffer OfferId="80000001">Some Offer</PrimaryOffer> <ChargeItem SortKey="YYYYY"> <SupplementaryOffer OfferId="100">Text <OtherElements></OtherElements> <AnotherElement></AnotherElement> </SupplementaryOffer> <SupplementaryOffer OfferId="101">Text <OtherElements></OtherElements> <AnotherElement></AnotherElement> </SupplementaryOffer> </ChargeItem> <ChargeItem SortKey="ZZZZ"> <SupplementaryOffer OfferId="200">Text <OtherElements></OtherElements> <AnotherElement></AnotherElement> </SupplementaryOffer> <SupplementaryOffer OfferId="201">Text <OtherElements></OtherElements> <AnotherElement></AnotherElement> </SupplementaryOffer> </ChargeItem> </ChargeItemList>
..there are multiple ChargeItemList elements.
Now, my query is on the Anonymous object property SOs. What I thought this would point to is a list of XElements (IEnumerable)<XElement>) but in fact it seems I have a collection and in that collection I have my "SupplementaryOffer" collection (IEnumerable)<XElement>.
So if iterate the chargeItemList,to get to the list of "SupplementaryOffer", I need to do something like
obj.Sos.FirstOrDefault()
I thought I could just do obj.Sos.Elements()
but then I would think I could do
obj.Sos.FirstOrDefault().Descendants("SupplementaryOffer")
but this does not work
If I just do this,
obj.Sos.FirstOrDefault().Elements()
I get a list of all the elements WITHIN the first "SupplementaryOffer"
I'm really confused as to what I am seeing, why I have a collection within a collection and why I cannot select Elements by name beneath?
Any help gratefully received
Mike
All Replies
-
Wednesday, March 06, 2013 9:47 AM
Apologies...I've just realised that the outer collection is actually
cis.Elements("ChargeItem")
by changing the linq to
SOs = from s in cis.Descendants("SupplementaryOffer").Where(c => c.Parent.Attribute("SortKey").Value == "SomeCategory") select sthis gets rid of the outer collection.
That's what happens when you code too late at night!
Thanks to anyone who was going to reply
Mike
- Marked As Answer by AgileChap Wednesday, March 06, 2013 9:47 AM
-
Thursday, March 07, 2013 1:25 AMModerator
Hi AgileChap,
Welcome to the MSDN forum.
Glad to know your problem is solved. Thank you for sharing.
Good day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

