-
Hi, I the way I thought it should work doesn't in 3.0. Am I missing something? When i use @KKica from #149 example adjusted to 3.0:
It's still skipping and taking the joined rows, which is usually the intended behaviour for Joins. Using Count with a Join in 3.0 does work and returns only the Count of the Parent Entities due to the DISTINCT Clause. Is there a way to do this? I tried changing the Order of the .Skip, .Top. OrderBy Calls, but still nothing. EDIT: My (temporary?) solution would be to just use a second query for preselection.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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}
ORDER BY {nameof(User.LastName):C}
-- the paging comes here
OFFSET 10 ROWS -- skip 10 rows
FETCH NEXT 10 ROWS ONLY -- take 10 rows
)
")
.WithParameters(myParamObject)
// repeat the OrderBy on the main entity here
.OrderBy($"nameof(User.LastName):TC")
); I'll add this to the wiki since this is a really common scenario. |
Beta Was this translation helpful? Give feedback.
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: