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?
source share