String.Equals() 未按预期工作

发布于 2024-10-18 17:45:25 字数 752 浏览 1 评论 0原文

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

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

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

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" 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();

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

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

发布评论

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

评论(7

昵称有卵用 2024-10-25 17:45:25

StringComparison.OrdinalIgnoreCase 的字符串比较在内存中进行,或与 IEnumerable 进行比较。您尝试将其与 IQueryable 一起使用,但可查询的提供者不理解它。

这对我有用:

db.Users.FirstOrDefault(
     s => s.Username.Equals(username, StringComparison.OrdinalIgnoreCase)
);

The string comparison with StringComparison.OrdinalIgnoreCase works in memory or with IEnumerable<T>. You are trying to use it with IQueryable<T>, but the provider of your queryable does not understand it.

This works for me:

db.Users.FirstOrDefault(
     s => s.Username.Equals(username, StringComparison.OrdinalIgnoreCase)
);
娇纵 2024-10-25 17:45:25

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

When using LINQ to Entities, it will automatically convert LINQ into 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.

诺曦 2024-10-25 17:45:25

使用String.Compare(),因为它可以转换为 Sql。

这里是一些示例Linq 中的字符串匹配以及 Sql 翻译。

Use the String.Compare() as it can be translated to Sql.

Here are some examples of string matching in Linq, with the Sql translation as well.

忱杏 2024-10-25 17:45:25

做了一些研究。你做不到。排序规则(比较类型)是在表的列级别定义的。您无法通过 EF 修改它。如果定义为不区分大小写,则所有搜索都将不区分大小写。如果它被定义为区分大小写,那么您唯一的希望就是 ToUpper() 字符串。

http://connect.microsoft.com/VisualStudio/feedback/details/435783/entity-framework-conceptual-model-doesnt-support-string-equals-via-linq

http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework /thread/3810aa67-f6fe-4624-a14b-eaaa0e05ddcd

EF4 Linq Oracle11g 使查询不区分大小写

LINQ to Entities 区分大小写比较< /a>

Made some research. You can't do. The collation (the type of comparison) is defined at the column level of the table. You can't modify it through EF. If it's defined as case insensitive, then all the searches will be case-insensitive. If it's defined as case sensitive, then your only hope is ToUpper() the strings.

http://connect.microsoft.com/VisualStudio/feedback/details/435783/entity-framework-conceptual-model-doesnt-support-string-equals-via-linq

http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/3810aa67-f6fe-4624-a14b-eaaa0e05ddcd

EF4 Linq Oracle11g making queries none case-sensitive

LINQ to Entities case sensitive comparison

噩梦成真你也成魔 2024-10-25 17:45:25

从技术角度来看,我喜欢 TravyGuy 的回答。
要获得更直接、实用的答案,请尝试使用:

string.Compare(string A, string B, StringComparison.OrdinalIgnoreCase) == 0

I like TravyGuy's answer from a technical perspective.
For a more direct, practical answer, try using:

string.Compare(string A, string B, StringComparison.OrdinalIgnoreCase) == 0
花心好男孩 2024-10-25 17:45:25

尝试 name.Equals(gr.Name, StringComparison.OrdinalIgnoreCase)

如果有效,则问题可能出在 gr.Name 上。

--- 编辑 ---

我猜测 gr.Name 不是 System.string 类型。 (因为 String.Equals 给你一个错误==>来自上一篇文章)

尝试一下

(gr.Name as string).Equals(name, StringComparison.OrdinalIgnoreCase)

或者

String.Equals((gr.Name as string), name, StringComparison.OrdinalIgnoreCase)

Try name.Equals(gr.Name, StringComparison.OrdinalIgnoreCase)

If it works then the problem could be with gr.Name.

--- Edit ---

I'm guessing that gr.Name is not of type System.string. (since String.Equals gives you an error ==> from the previous post)

give this a shot

(gr.Name as string).Equals(name, StringComparison.OrdinalIgnoreCase)

or

String.Equals((gr.Name as string), name, StringComparison.OrdinalIgnoreCase)
ι不睡觉的鱼゛ 2024-10-25 17:45:25

试试这个!

EF.Functions.Collate(gr.Name, "SQL_Latin1_General_CP1_CI_AS") == name

Try this!

EF.Functions.Collate(gr.Name, "SQL_Latin1_General_CP1_CI_AS") == name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文