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

Ordering by a computed column #13

Closed
kieranbenton opened this issue Feb 16, 2022 · 2 comments
Closed

Ordering by a computed column #13

kieranbenton opened this issue Feb 16, 2022 · 2 comments

Comments

@kieranbenton
Copy link

Hi,
Found my way here via dotnet/efcore#20967 (comment) and frankly your project is brilliant and has solved our pagination woes in a repeatable and consistent way.

I have one corner case that I was hoping to discuss how it might be solved (I can attempt a PR for it if necessary). We would like to be able to order by an EF Core 'computed column'. Something like:

	[NotMapped]
	public short status_order { get; set; }
			modelBuilder.Entity<OurTable>()
				.Property(x => x.status_order)
				.HasComputedColumnSql(@"
					CASE status
						WHEN 0 THEN 0
						WHEN 3 THEN 1
						WHEN 2 THEN 2
						WHEN 1 THEN 3
						WHEN 99 THEN 99
						ELSE -1
					END
				");
			var keysetContext = ourTable
				.KeysetPaginate(
					b => b.Ascending(p => p.status_order),
					KeysetPaginationDirection.Forward,
					reference 
				);

                        var items = await paginationContext
			    .Query
			    .Take(pageSize)
			    .ToListAsync(cancellationToken);

Right now this builds a SQL query with an ORDER BY clause that directly refers to the computed (not actually existing) column:

...
ORDER BY e.status_order

Which obviously does not work.

So initially by query is two-fold:

  1. Is this even possible at all right now with EF Core?
  2. If yes, can you give me some pointers as to how you might go about attacking this so I can have a go myself?
@kieranbenton kieranbenton changed the title Ordering by a computed problem Ordering by a computed column Feb 16, 2022
@mrahhal
Copy link
Owner

mrahhal commented Feb 16, 2022

Hi. Glad it was of help.

So, for the query to work, the generated sql shouldn't be qualifying it with e.. This library only dynamically creates the comparison expressions and feeds them back to EF, it's not involved in translating anything, so the problem here is from EF itself most likely.

Does this work if you don't use KeysetPaginate at all (and just use an OrderBy directly) or does the same thing happen? If the same happens, that means maybe it's an EF bug? It seems to me this should work as is from the code you've shown, so I feel it's an EF translation bug.

@kieranbenton
Copy link
Author

You're right, dropping MR.EntityFrameworkCore.KeysetPagination out completely and doing an OrderBy through EF Core generates the 'bad' SQL against the non-existent column - thanks for the pointer!

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