문제

I just discovered some strange behaviour running a query in LinqPad. It seems that some ASCII symbols/characters are considered equal by LinqToSQL when they actually are not.

Consider the following class (with a corresponding table):

public class SpecialCharacter
{
    int Id;
    string Character;
    string Name;
}

If you add a row to your table for "⇒"(right arrow) and "⇐" (left arrow) the following query will tell you they are equal (result will contain two items):

var result = from a in SpecialCharacters
             from b in SpecialCharacters
             where a.Character == b.Character  && a != b
             select new {A = a, B = b};

Changing the query by adding a call to .ToList() will result in the expected behaviour (result is empty):

var result = from a in SpecialCharacters.ToList()
             from b in SpecialCharacters.ToList()
             where a.Character == b.Character && a != b
             select new {A = a, B = b};

Any ideas how this comes about?

도움이 되었습니까?

해결책

The difference you are seeing is due to differences between how LINQ handles equality and SQL server's collation settings. You would see the same thing if you did a case sensitive search. LINQ to Objects. Consider the following query:

var query = from c in Customers
            where c.City == "london"
            select c;

LINQ to Objects would match cities that are london, but not London or LONDON. LINQ to SQL or EF on the other hand would default to matching any of those three because it is case insensitive. You may want to check the generated TSQL in LINQ to SQL by inspecting the result.ToString to see the SQL generated and then run that SQL directly in SSMS. You should see the same extra records returned. You will probably need to tweak the collation settings or other database specific settings to get the correct behavior on this query.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top