Skip to content

Proper Pagination with Joins #170

Answered by MoonStorm
dblex asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, you have to be careful when you're paginating with Parent/Child relationships. Parents get duplicated when coming as rows in the result set from the database with the children. FastCrud will remove the duplicates, but by that time the SKIP and TOP clauses where already executed by the DB.

Your temporary solution works but could be improved. In situations like these you need to filter and perform the pagination in a subquery:

var records = _db.FindAsync<User>(x => x
    .Include<Roles>(o => o.LeftOuterJoin())
    .Where($@"{nameof(User.Id):TC} IN 
	   (
			SELECT {nameof(User.UserId):C}
			FROM {nameof(User):T}
			WHERE {nameof(User.FirstName):C} = {nameof(myParamObject.FirstName):P}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dblex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants