0

I have a piece of code:

var component = (from components in dataContext.Component select components)
                    .Where(x => x.Name.ToString().Equals(componentCode));

where Component is very simple object.

In mysql database table (encoded in utf8 - utf8_unicode_ci)

I have record such as: szkło 3mm (with polish character ł).

When I'm trying to find this record with the code above I get nothing.

Records without polish characters works fine.But when I'm doing it with foreach I get the result:

var component = (from components in dataContext.Component select components);
foreach(Component comp in component)
{
    if (comp.Name.ToString().Equals(componentCode))
        componentPrice = (decimal)comp.Price;
}

Is there a way to get the result with just a linq code without iterating through table.?

6
  • 1
    Any reason you are using varchar and not nvarchar for the Name column? Commented Jun 22, 2016 at 18:35
  • 1
    What is the datatype of Component.Name in your DB. Is it nvarchar? Commented Jun 22, 2016 at 18:36
  • Type of Name is varchar. And no, there is no reason to use varchar. Commented Jun 22, 2016 at 19:09
  • Interesting that you are having the opposite problem that most people have. The linq query is doing a sql compare (which is failing apparently), whereas the for loop is forcing the execution of the query into memory, then doing a .Net comparison. Usually the sql comparison is more forgiving because it is case insensitive by default. Commented Jun 22, 2016 at 19:21
  • the problem seems to be the encoding, I kind of doubt EF can calculate a query with a parameter containing such symbols. Try to set the encoding of your column to unicode (either via connection string, fluent API or Data annotation). Commented Jun 22, 2016 at 19:23

1 Answer 1

1

Ok, I have found solution - I have added "Charset=utf8" in the connection string and it works. Solution found in topic: Entity Framework C# Insert Data russian encoding problems

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.