XML:
<DLC>
<ObjectClass>RVM_GWR_Class166_DMSL_C</ObjectClass>
<ShortName>Class166</ShortName>
</DLC>
<DLC>
<ObjectClass>RVM_BR_Class170_DMCL_W_C</ObjectClass>
<ShortName>Class170</ShortName>
</DLC>
Previous code:
var values = xml.Descendants("DLC")
.Where(i => i.Element("ObjectClass").Value == locoName)
.Select(DLC => new
{
ShortName = (string)DLC.Element("ShortName"),
DisplayName = (string)DLC.Element("DisplayName"),
});
Currently, the code works by checking whether the locoName
string matches the ObjectClass
XML element.
But what I really need to do is something like this:
.Where(locoName.Contains(i => i.Element("ShortName").Value))
The above does not compile due to:
Cannot convert lambda expression to type ‘string’ because it is not a delegate type
I need to make a match where the locoName
string contains the ShortName
XML Element.
The locoName
will be something like this: “RVM_GWR_Class166_Z_ABC123” ie; the Class166
part will match.
I can’t quite work out how to do that in LINQ.
Your .Where
expression is incorrect. It should be:
.Where(x => locoName.Contains(x.Element("ShortName").Value))
to check the locoName
contains the ShortName
property value.
var values = xml.Descendants("DLC")
.Where(x => locoName.Contains(x.Element("ShortName").Value))
.Select(DLC => new
{
ShortName = (string)DLC.Element("ShortName"),
DisplayName = (string)DLC.Element("DisplayName"),
});
2