Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:44:25+00:00 2026-05-19T04:44:25+00:00

I am trying to get a row from a table using the entity framework

  • 0

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.

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.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:44:25+00:00Added an answer on May 19, 2026 at 4:44 am

    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.

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

    As stated in 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.

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I'm trying to get a specific cell from a table view so I can
I am trying to insert a row in ms-sql table using the following code
I am trying to get the DataGridView to render the insert new row row
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Im trying to get a completly data copy from a gridview, itryed clone(), tryed
I have a DataForm where I set the CurrentItem to an Entity returned from
I'm trying to execute a basic INSERT statement on a MySQL table from a
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.