实体框架中的字符串匹配问题.适用于字符串文字,但不适用于字符串变量 [英] String matching problem in Entity framework. Works for a string literal but not for a string variable

查看:89
本文介绍了实体框架中的字符串匹配问题.适用于字符串文字,但不适用于字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#中的实体框架从表中获取一行.我有一个名为"TipoPlanta"的表,该表的主键为"Tipo",其类型为字符串.

I am trying to get a row from a table using the entity framework in C#. I have a table called "TipoPlanta" with a primary key called "Tipo" which is of type String.

当我尝试使用字符串从表中获取一行时,只有使用字符串文字时,我才能找到某些内容.如果我使用传递给该方法的字符串,则找不到行.

When I try to get a row from the table usng a String I can only find something if I use a string literal. If I use the string passed to the method I find no rows.

我有以下方法,其中有一些我一直在尝试调试的附加内容.我传递了字符串tipoString,该字符串在此示例中的值为"Arbol持久性".这是代码:

I have the following method which has a bit of added stuff that I have been trying to debug. I pass the String tipoString which in this example has a value of "Arbol persistente". Here's the code:

        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;
        }
    }

输出为: 他们是一样的

The output is: They are the same

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

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

As can be seen when I use the string literal it finds the row but not when I use the string I passed even though they appear to be the same.

有什么想法吗?

推荐答案

您似乎遇到了编码问题. tipoString和您的字符串常量在调试器中可能看起来相同,而==可能返回true,但是其中某些字符使用不同的编码.

You seem have an encoding issue. tipoString and your string constant may look the same in debugger and == may return true, but have some characters in different encodings.

当将tipoString与字符串常量进行比较时,请使用string.Compare(tipoString,"Arbolpersiste",StringComparison.CurrentCulture);而不是==运算符.

When you compare tipoString to a string constant, please use string.Compare(tipoString, "Arbol persistente", StringComparison.CurrentCulture); instead of == operator.

C#编程指南所述:

比较字符串时,应使用显式指定要执行的比较类型的方法.这使您的代码更具可维护性和可读性.尽可能使用带有StringComparison枚举参数的System.String和System.Array类的方法的重载,以便您可以指定要执行的比较类型.比较字符串时,最好避免使用==和!=运算符.另外,请避免使用String.CompareTo实例方法,因为任何重载都不会占用StringComparison.

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中有数据).

If this does not help, then I'm agree with Daniel - please see what actual SQL statement is executed using SQL Server Profiler (assuming you have your data in SQL Server).

这篇关于实体框架中的字符串匹配问题.适用于字符串文字,但不适用于字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆