Can someone shed some light on thsi query?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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"
Im 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

View the full article
 
Back
Top