I am extracting data from a xml document into a structure using an xElement query. I would like to limit the data being stored to only when certain fields contain part of a particular string.
This code works, but the string has to be an exact match.
var data = from query in xmlDocFromPage.Descendants("DIRECTORY")
where (string)query.Element("lastNAME") == "Smith"
select new contactDataClass
{
lastName = (string)query.Element("lastNAME"),
firstName = (string)query.Element("firstNAME"),
middleName = (string)query.Element("middleNAME")
};
I am trying to achieve the equivalent of the following sql looking statment.
where (string)query.Element("lastNAME") like "%Smith%"
Is it possible?
You can use the string.Contains
method (after null-checking in case the node doesn't exist):
where query.Element("lastNAME") != null && query.Element("lastNAME").Value.Contains("Smith")