String.Equals() 不按预期工作 [英] String.Equals() not working as intended

查看:26
本文介绍了String.Equals() 不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LINQ 搜索我的实体框架表之一,并根据名称查找组".该名称是一个字符串,似乎是 Unicode(说它在 edmx 中).我有一个方法 GetGroup() 并传入一个名称进行搜索.通过代码调试,我的数据库中已经有一个名为Test"的组.一旦我传入一个名为TEST"的组,我希望它返回数据库中已经存在的Test".由于某种原因,它没有找到测试"并认为测试"不存在.这是我的查询,我不明白为什么它不起作用.请帮忙.

I am using LINQ to search through one of my Entity Framework tables and find a "group" based on the name. The name is a string and appears to be Unicode (says it is in the edmx). I have a method GetGroup() and I pass in a name to search for. Debugging through the code, I already have a group named "Test" in my database. Once I pass in a group named "TEST" I expect it to return the "Test" which was already in the database. It for some reason, does not find the "Test" and thinks "TEST" doesn't exist. Here is my query, I cannot see why it does not work. Please help.

"name" 是传入的组名.我的 .Equals 似乎只有在 gr.Name 和名称完全相同时才有效.如果两个字符串之一中的一个字符是大写的,则 .Equals 不起作用.我曾尝试使用 InvariantCultureIgnoreCase,但这似乎没有帮助.如果有人询问,MyLeagueIdLeagueId 将始终匹配,数据库已设置,因此可以在不同的联赛 ID 中存在一个组.我不认为这是问题所在.

"name" is the passed in the group name. My .Equals seems to only work if the gr.Name and name are the exact same. If one character is capital in one of the two strings, then the .Equals doesn't work. I have tried to use InvariantCultureIgnoreCase, and that did not seem to help. In case someone asks, the MyLeagueId and LeagueId will always match, the database is setup so there can be a group in a different league id. I do not think this is the problem.

Group g = (from gr in this.DatabaseConnection.Groups
           where gr.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
           gr.LeagueId == this.MyLeagueId
           select gr).FirstOrDefault();

推荐答案

使用 LINQ to Entities 时,会自动将其转换为 LINQ to SQL.如果您正在执行 .Equals 的数据库字段没有 NOCASE(在我的示例中为 SQLite)的整理,那么它将始终区分大小写.换句话说,数据库定义了如何进行字符串比较而不是代码.

When using LINQ to Entities, it will automatically convert it to LINQ to SQL. And if the database field you are doing a .Equals on does not have a collate of NOCASE (SQLite in my example) then it will always be case-sensitive. In otherwords, the database defines how to do the string comparison rather than code.

这篇关于String.Equals() 不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆