LINQ查询:如何做大小写不敏感查询?

时间:2022-09-23 12:41:02

Is there a toupper or tolower or similar functions I can use to do a case-insensitive LINQ to XML query? If so, how do you revise the following line of code to do a case-insensitive query? Thanks.

我是否可以使用toupper或tolower或类似的函数来执行不区分大小写的LINQ到XML查询?如果是,如何修改以下代码行来执行不区分大小写的查询?谢谢。

User types search word into textbox and I do this in LINQ:

用户将搜索词输入文本框,我在LINQ中这样做:

where d.Element("ProductName").Value.Contains(textBox1.Text)

2 个解决方案

#1


2  

where d.Element("ProductName").Value.ToLower().Contains(textBox1.Text.ToLower())

You probably should do some sort of null check though, just in case.

你可能应该做一些零校验,以防万一。

#2


4  

It is very simple (and not correct)

它非常简单(而且不正确)

where d.Element("ProductName").Value.Equals(
  textBox1.Text, StringComparison.InvariantCultureIgnoreCase) 

Ok, edited - more on MSDN:

好的,编辑-更多关于MSDN:

where d.Element("ProductName").Value.IndexOf(
  textBox1.Text, StringComparison.InvariantCultureIgnoreCase) > 0

#1


2  

where d.Element("ProductName").Value.ToLower().Contains(textBox1.Text.ToLower())

You probably should do some sort of null check though, just in case.

你可能应该做一些零校验,以防万一。

#2


4  

It is very simple (and not correct)

它非常简单(而且不正确)

where d.Element("ProductName").Value.Equals(
  textBox1.Text, StringComparison.InvariantCultureIgnoreCase) 

Ok, edited - more on MSDN:

好的,编辑-更多关于MSDN:

where d.Element("ProductName").Value.IndexOf(
  textBox1.Text, StringComparison.InvariantCultureIgnoreCase) > 0