LINQ 如何搜索 UNICODE 值

发布于 2024-10-29 08:23:02 字数 650 浏览 2 评论 0原文

帮助我在 LINQ 中搜索 UNICODE 值,当我编写 LINQ 来搜索不是 UNICODE 的字符串时,它工作正常,但字符串是 UNICODE,我的查询不会搜索到任何内容。

Menus.Where(e => e.Name.ToLower().Contains(keyword.ToLower()))
     .OrderBy(e => e.Order)
     .ThenBy(e => e.Name)
     .ToList();

它在没有 Unicode 字符串的情况下工作得很好

"AMERICAN BREAFAST: CHOICE OF FRIED EGG OR OMELETE SERVED WITH CRISPY BACON, SAUSAGE, SAUTEED TOMATO AND TOAST" 

,但是当我搜索像这样的 Unicode 字符串时,

"Bữa ăn sáng kiểu Mỹ: chọn giữa trứng chiên hoặc trứng cuộn dùng kèm thịt xông khói, xúc xích, cà chua xào và bánh mì nướng" 

它就不再工作了。请帮我解决它。非常感谢

Help me to search UNICODE values in LINQ, when i write a LINQ to search a string not UNICODE , it work fine but string is UNICODE, my query does not search out anything.

Menus.Where(e => e.Name.ToLower().Contains(keyword.ToLower()))
     .OrderBy(e => e.Order)
     .ThenBy(e => e.Name)
     .ToList();

It work fine with none Unicode string like

"AMERICAN BREAFAST: CHOICE OF FRIED EGG OR OMELETE SERVED WITH CRISPY BACON, SAUSAGE, SAUTEED TOMATO AND TOAST" 

but when i search a unicode string like

"Bữa ăn sáng kiểu Mỹ: chọn giữa trứng chiên hoặc trứng cuộn dùng kèm thịt xông khói, xúc xích, cà chua xào và bánh mì nướng" 

it does not work anymore. please help me fix it. thanks so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆沫 2024-11-05 08:23:03

我无法重现您的问题,但是由于 string.Contains 使用序数区域性进行比较,因此它可能错误地匹配了某些字符。您可能需要将 where 子句更改为 Where(e => e.IndexOf(keyword,StringComparison.CurrentCultureIgnoreCase)>=0) 以避免文化问题。

I wasn't able to reproduce your issue, however it's possible that since string.Contains uses the ordinal culture for comparisons, it's incorrectly matching some characters. You may want to change your where clause to Where(e => e.IndexOf(keyword,StringComparison.CurrentCultureIgnoreCase)>=0) to avoid culture issues.

痴情 2024-11-05 08:23:02

首先,你提供的英文字符串实际上是Unicode的。

其次,这一类问题的根源有几个可能的定位:

  1. “关键词”从何而来?它是否正确地从源编码转换为 Unicode?例如,如果数据来自 ASP.Net 应用程序,则需要确保 HTML 元内容类型标记为“text/html; charset=UTF-8”,并且服务器请求和响应编码为也默认为 UTF-8。如果您可以在调试器中毫无问题地查看此变量关键字,那么您可能没问题。
  2. 基础数据源中的 e.Name 是什么类型?如果在 SQL Server 上,它是 NVARCHAR 还是 VARCHAR?如果是 VARCHAR,排序规则是否映射到支持越南语的合适编码?
  3. 无论 NVARCHAR 还是 VARCHAR,底层排序规则也隐含区分大小写的规则。如果您的字段使用区分大小写的排序规则,则您的 toLower 调用可能会导致意外结果,因为它与数据库中的排序规则不匹配。此外,字段的排序规则指定比较是区分重音还是不区分重音,这可能会影响您的结果。
  4. 在某些情况下,Unicode 规范化形式可能会影响您的结果。由于越南语可以用组合字符或分解字母 + 变音符号代码点表示,因此您应该确保规范化是一致的。例如,您可能会发现最好编码为 un​​icode kC 或 kD 形式,具体取决于您的偏好,但您应该始终如一地这样做。

First of all, the English string you provided is actually Unicode.

Second, this category of problem has several possible loci for the source of the problem:

  1. Where does "keyword" come from? Was it correctly converted from the source encoding to Unicode? If the data came from, for example, an ASP.Net application, you need to make sure that the HTML Meta Content-Type tag is "text/html; charset=UTF-8", and that the server Request and Response encoding is also defaulting to UTF-8. If you can view this variable, keyword, in the debugger without trouble, then you're probably fine.
  2. What's the type of e.Name in the underlying data source? If it's on SQL Server, is it an NVARCHAR or VARCHAR? If it's VARCHAR, does the collation map to a suitable encoding that supports Vietnamese?
  3. Regardless of NVARCHAR or VARCHAR, is the underlying collation also implies case sensitivity rules. If your field is using a collation that is case sensitive, your toLower call may cause unexpected results since it won't match the collation rules in the database. Additionally, the field's collation specifies whether comparisons are accent sensitive or accent insensitive, which can affect your results.
  4. Under some circumstances, the Unicode normalization form may affect your results.Because Vietnamese can be represented with composed characters or decomposed letters + diacritic codepoints, you should make sure that your normalization is consistent. For example, you may find it best to encode to unicode kC or kD form, depending on your preference, but you should do that consistently.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文