I create an IEnumerable object that just contains the nodes I want from an xml file:
IEnumerable<XElement> rosters = XDocument.Load('roster.xml') .Elements('rosterlist') .Elements('roster') .Where(w => w.Element('division') .Value .Equals('SUPER AWESOME DIVISION'));
So it’s a collection of these:
<rosterlist> <roster> <userid>1</userid> <name></name> <etc></etc> </roster> <roster> <userid>2</userid> <name></name> <etc></etc> </roster> </rosterlist>
I want to grab only the users where the userid attribute is also a userid node within the rosters collection.
IEnumerable<XElement> users = XDocument.Load('user.xml') .Elements('userlist') .Elements('user') .Where(w => rosters.Elements('userid') .Contains(w.Attribute('userid').Value));
But it is giving me an error:
The type arguments for method ‘System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.
What is wrong with my approach?
One issue I see is that in the last code snippet,
...Elements('userid')returns a list of XElement objects which cannot contain a String returned by the Value property. This should work…However I’d do this by using a join query.. Select user join rosters on userid
It’d go something like this
Outputs: