String matching issue in Entity framework. Works for a string literal, but not for a string variable

I am trying to get a row from a table using an entity structure 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 string from a usring String table, I can only find something if I use a string literal. If I use the string passed to the method, I cannot find any strings.

I have the following method that contains some added things that I was trying to debug. I am passing String tipoString, which in this example has the value "Arbol persistente". Here is 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; } } 

Exit: 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 you can see, when I use a string literal, it finds the string, but not when I use the passed string, although they seem the same.

Any ideas?

+4
source share
1 answer

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

When you match tipoString with a string constant, use string.Compare (tipoString, "Arbol persistente", StringComparison.CurrentCulture); instead of the operator ==.

As stated in the C # Programming Guide :

When comparing strings, you should use methods that explicitly indicate which comparison you intend to perform. This makes your code much more convenient and readable. If possible, use the overloads of the methods of the System.String and System.Array classes that take the StringComparison enumeration parameter so that you can specify what type of comparison to perform. It is better to avoid using the == and! = Operators when comparing strings. Also avoid using instance methods of String.CompareTo, because none of the overloads accepts the StringComparison parameter.

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

+2
source

Source: https://habr.com/ru/post/1333724/


All Articles