简体   繁体   中英

Difference in Where clause and foreach look up in C# entity framework

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM