I have an IEnumerable<Item> where Item has a property named “widgets” which contains xml as a string. The xml contains elements for each widget and each widget can contain an optional element named ‘foo’. For example:
<widgets>
<widget code="A">
<foo>1</foo>
</widget>
<widget code="B" />
</widgets>
Can I flatten the IEnumerable<Item> into an IEnumerable of an anonymous type which represents a widget?
I’m not sure if
foocan be repeating element or not, but in this case it’s returned as anEnumerable<String>, which is simple enough to change.The setup (here there are two items, with the same
WidgetsXML):The widget selection process:
That gives you 4 items (I1 A, I1 B, I2 A, I2 B) with Name (from Item) and Code and Foo (from the XML) set correctly.
If you want
footo be a single element, the change is as follows (settingFirstOrDefaultbasically), which will give you nulls for whenfooisn’t around: