Reputation: 4344
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();
Upvotes: 48
Views: 53044
Reputation: 4344
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.
Upvotes: 46
Reputation: 11
Try this!
EF.Functions.Collate(gr.Name, "SQL_Latin1_General_CP1_CI_AS") == name
Upvotes: 0
Reputation: 620
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)
);
Upvotes: 48
Reputation: 5383
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.
Upvotes: 15
Reputation: 2595
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)
Upvotes: 2
Reputation: 6822
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
Upvotes: 6
Reputation: 111810
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.
EF4 Linq Oracle11g making queries none case-sensitive
LINQ to Entities case sensitive comparison
Upvotes: 10