Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ中有OR子句吗?_Linq - Fatal编程技术网

LINQ中有OR子句吗?

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

我正在尝试查询XML文档以获取所需的特定记录。我知道下面包含“or where”的行是不正确的,但我希望它能说明我正在努力实现的目标。可以对两个不同的属性执行条件where子句吗

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)
                         ..