LINQ中有OR子句吗?
我正在尝试查询XML文档以获取所需的特定记录。我知道下面包含“or where”的行是不正确的,但我希望它能说明我正在努力实现的目标。可以对两个不同的属性执行条件where子句吗LINQ中有OR子句吗?,linq,Linq,我正在尝试查询XML文档以获取所需的特定记录。我知道下面包含“or where”的行是不正确的,但我希望它能说明我正在努力实现的目标。可以对两个不同的属性执行条件where子句吗 XDocument xd = XDocument.Load("CardData.xml"); SearchList.ItemsSource = from x in xd.Descendants("card") where x.Element("title").Value
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
};
是-只需使用布尔或| |并将您的条件组合成一个
Where
子句:
where x.Element("title").Value.ToUpper().Contains(searchterm.ToUpper()) ||
x.Element("id").Value.Contains(searchterm)
另请注意,作为一个小优化,我会预先计算您当前在Where子句中的一些操作,以便它们不会在列表中的每个项目上执行-可能没关系,但当您有很多元素时(我认为这只是一个好习惯):
string searchTermUpperCase = searchterm.ToUpper();
SearchList.ItemsSource = from x in xd.Descendants("card")
where x.Element("title").Value.ToUpper().Contains(searchTermUpperCase)
or where x.Element("id").Value.Contains(searchterm)
..