Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderBy() and OrderByDescending() return wrong results when used with bool properties #42

Open
Ahmed-Abdelhameed opened this issue Mar 17, 2019 · 2 comments

Comments

@Ahmed-Abdelhameed
Copy link

Ahmed-Abdelhameed commented Mar 17, 2019

The following query:
context.Items.OrderBy(x => x.IsActive);
..returns items with IsActive == true first then those with IsActive == false (should be the opposite).

Similarily, the following query:
context.Items.OrderByDescending(x => x.IsActive);
..returns items with IsActive == false first then those with IsActive == true (should be the other way around).

This seems to be a bug in JetEntityFrameworkProvider because it works the right way with SQL Server and, of course, with any other collection. As a workaround, I'm currently using context.Items.ToList().OrderBy....

@jeremy-morren
Copy link

This is because SQL Server uses Bit (1 or 0) for boolean values. Therefore EntityFramework must use ORDER BY [field] ASC in the SQL Statement.

However, JetEntity uses the "Yes/No" Access field for boolean values. Here, "Yes" is a value of -1, therefore they appear first.

A better workaround is context.Items.OrderBy(x => !x.IsActive).

See https://stackoverflow.com/questions/8827447/why-is-yes-a-value-of-1-in-ms-access-database for more details.

@Ahmed-Abdelhameed
Copy link
Author

Ahmed-Abdelhameed commented Mar 22, 2019

@jeremy-morren I do know that a "Yes/No" field in Access is represented as 0 or -1 value. However, since it's mapped by EF as a .NET Boolean type, it should be consistent with the Boolean type of the .NET Framework. EF is an ORM after all and works as a separation layer, therefore, it should (and it does) handle such conflicts behind the scenes (one example off the top of my head is that EF translates null to DBNull and vice-versa).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants