c# – 实体框架中的字符串匹配问题.适用于字符串文字,但不适用于字符串变量

我试图在C#中使用实体框架从表中获取一行.我有一个名为“TipoPlanta”的表,其中一个名为“Tipo”的主键是String类型.

当我尝试从表中获取一行时使用字符串我只能找到一些东西,如果我使用字符串文字.如果我使用传递给方法的字符串,我找不到行.

我有以下方法,它有一些我一直在尝试调试的添加的东西.我传递了String tipoString,在这个例子中,它的值为“Arbol persistente”.这是代码:

        private TipoPlanta getTipoPlanta(String tipoString)
    {
        try
        {
            if (tipoString == "Arbol persistente")
                Console.WriteLine("They are the same");
            else
                Console.WriteLine("They are different");

            var result = (from tar in plantaContext.TipoPlanta where tar.Tipo.Contains(tipoString) select tar);
            var sql = ((System.Data.Objects.ObjectQuery)result).ToTraceString();
            Console.WriteLine("SQL = " + sql);
            Console.WriteLine("RESULT COUNT = " + result.Count());
            Console.WriteLine();

            var resultLiteral = (from tar in plantaContext.TipoPlanta where tar.Tipo.Contains("Arbol persistente") select tar);
            var sql2 = ((System.Data.Objects.ObjectQuery)resultLiteral).ToTraceString();
            Console.WriteLine("SQL2 = " + sql2);
            Console.WriteLine("RESULT LITERAL COUNT = " + resultLiteral.Count());

            TipoPlanta tipo = result.First<TipoPlanta>();
            //              TipoPlanta tipo = (from tar in plantaContext.TipoPlanta where tar.Tipo.Contains("Arbol persistente") select tar).First();
            //TipoPlanta tipo = (from  in plantaContext.TipoPlanta where t.Tipo.CompareTo(tipoString.Trim()) == 0 select t).First();
            return tipo;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            log("Tipo", tipoString, "no existe.");
            return null;
        }
    }

输出是:
他们是一样的

SQL = SELECT
[Extent1].[Tipo] AS [Tipo]
FROM [TipoPlanta] AS [Extent1]
WHERE (CHARINDEX(@p__linq__1, [Extent1].[Tipo])) > 0
RESULT COUNT = 0

SQL2 = SELECT 
[Extent1].[Tipo] AS [Tipo]
FROM [TipoPlanta] AS [Extent1]
WHERE (CHARINDEX(N'Arbol persistente', [Extent1].[Tipo])) > 0
RESULT LITERAL COUNT = 1

可以看出,当我使用字符串文字时,它会找到行而不是当我使用我传递的字符串时,即使它们看起来是相同的.

有任何想法吗?

最佳答案 你好像有编码问题. tipoString和你的字符串常量在调试器中可能看起来相同,==可能返回true,但是在不同的编码中有一些字符.

当你将tipoString与一个字符串常量进行比较时,请使用string.Compare(tipoString,“Arbol persistente”,StringComparison.CurrentCulture);而不是==运算符.

C# Programming Guide所述:

When you compare strings, you should use the methods that explicitly specify what kind of comparison you intend to perform. This makes your code much more maintainable and readable. Whenever possible, use the overloads of the methods of the System.String and System.Array classes that take a StringComparison enumeration parameter, so that you can specify which type of comparison to perform. It is best to avoid using the == and != operators when you compare strings. Also, avoid using the String.CompareTo instance methods because none of the overloads takes a StringComparison.

如果这没有帮助,那么我同意Daniel – 请查看使用SQL Server Profiler执行的实际SQL语句(假设您在SQL Server中有数据).

点赞