I am trying to query an XML document for the specific records that I need. I know that the line containing the “or where” case below is incorrect, but I’m hoping it will illustrate what I am trying to accomplish. Can you do a conditional where clause on two seperate properties?
XDocument xd = XDocument.Load("CardData.xml");
SearchList.ItemsSource = from x in xd.Descendants("card")
where x.Element("title").Value.ToUpper().Contains(searchterm.ToUpper())
or where x.Element("id").Value.Contains(searchterm)
select new Card
{
Title = x.Element("title").Value
};
Yes – simply use the boolean or || and combine your conditions into one
Whereclause:Also note just as a minor optimization, I would pre-compute some of the operations you currently have in your Where clause so they are not performed on every item in the list – probably doesn’t matter but it might when you have a lot of elements (and is just a good habit to get into in my opinion):