String.Equals() 未按预期工作
我正在使用 LINQ 搜索我的实体框架表之一,并根据名称找到一个“组”。该名称是一个字符串,并且似乎是 Unicode(表示它在 edmx 中)。我有一个方法 GetGroup()
,并传入一个名称进行搜索。通过代码调试,我的数据库中已经有一个名为“Test”的组。一旦我传入一个名为“TEST”的组,我希望它返回数据库中已经存在的“Test”。由于某种原因,它找不到“测试”并认为“测试”不存在。这是我的查询,我不明白为什么它不起作用。请帮忙。
“name”是传入的组名称。我的 .Equals
似乎只有在 gr.Name
和名称完全相同的情况下才有效。如果两个字符串之一中有一个字符为大写,则 .Equals 不起作用。我尝试使用 InvariantCultureIgnoreCase,但这似乎没有帮助。如果有人询问,MyLeagueId
和 LeagueId
将始终匹配,数据库已设置,因此可以有一个不同联盟 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
与
StringComparison.OrdinalIgnoreCase
的字符串比较在内存中进行,或与IEnumerable
进行比较。您尝试将其与IQueryable
一起使用,但可查询的提供者不理解它。这对我有用:
The string comparison with
StringComparison.OrdinalIgnoreCase
works in memory or withIEnumerable<T>
. You are trying to use it withIQueryable<T>
, but the provider of your queryable does not understand it.This works for me:
当使用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.
使用
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.
做了一些研究。你做不到。排序规则(比较类型)是在表的列级别定义的。您无法通过 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
从技术角度来看,我喜欢 TravyGuy 的回答。
要获得更直接、实用的答案,请尝试使用:
I like TravyGuy's answer from a technical perspective.
For a more direct, practical answer, try using:
尝试
name.Equals(gr.Name, StringComparison.OrdinalIgnoreCase)
如果有效,则问题可能出在
gr.Name
上。--- 编辑 ---
我猜测
gr.Name
不是System.string
类型。 (因为String.Equals
给你一个错误==>来自上一篇文章)尝试一下
或者
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 typeSystem.string
. (sinceString.Equals
gives you an error ==> from the previous post)give this a shot
or
试试这个!
Try this!