I am doing a query on the userauth and userauthrole table to find users who have a certain role. The result is paginated, so Skip and Take are also used:
var query = Db.From<UserAuth>();
query = query.LeftJoin<UserAuthRole>((a, b) => a.Id == b.UserAuthId)
.Where<UserAuthRole>(r => "role" == r.Role);
.Skip(2);
.Take(2);
.OrderBy("displayName")
.SelectDistinct();
var list = Db.Select<UserAuth>(query);
This creates the following SQL (SQL Server):
SELECT "UserAuth"."Id", "UserName", "Email", "PrimaryEmail", "PhoneNumber", "FirstName", "LastName", "DisplayName", "Company", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "FullName", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "Salt", "PasswordHash", "DigestHa1Hash", "Roles", "Permissions", "CreatedDate", "ModifiedDate", "InvalidLoginAttempts", "LastLoginAttempt", "LockedDate", "RecoveryToken", "RefId", "RefIdStr", "Meta" FROM (SELECT ROW_NUMBER() OVER (
ORDER BY displayName) As RowNum, "UserAuth"."Id", "UserAuth"."UserName", "UserAuth"."Email", "UserAuth"."PrimaryEmail", "UserAuth"."PhoneNumber", "UserAuth"."FirstName", "UserAuth"."LastName", "UserAuth"."DisplayName", "UserAuth"."Company", "UserAuth"."BirthDate", "UserAuth"."BirthDateRaw", "UserAuth"."Address", "UserAuth"."Address2", "UserAuth"."City", "UserAuth"."State", "UserAuth"."Country", "UserAuth"."Culture", "UserAuth"."FullName", "UserAuth"."Gender", "UserAuth"."Language", "UserAuth"."MailAddress", "UserAuth"."Nickname", "UserAuth"."PostalCode", "UserAuth"."TimeZone", "UserAuth"."Salt", "UserAuth"."PasswordHash", "UserAuth"."DigestHa1Hash", "UserAuth"."Roles", "UserAuth"."Permissions", "UserAuth"."CreatedDate", "UserAuth"."ModifiedDate", "UserAuth"."InvalidLoginAttempts", "UserAuth"."LastLoginAttempt", "UserAuth"."LockedDate", "UserAuth"."RecoveryToken", "UserAuth"."RefId", "UserAuth"."RefIdStr", "UserAuth"."Meta"
FROM "UserAuth" LEFT JOIN "UserAuthRole" ON ("UserAuth"."Id" = "UserAuthRole"."UserAuthId")
WHERE (N'my role' = "UserAuthRole"."Role")) AS RowConstrainedResult WHERE RowNum > 2 AND RowNum <= 4
which creates an SqlException
"The multi-part identifier "UserAuth.Id" could not be bound."
It seems that adding the Skip (or Take) is causing the problem as it works without them.
Is there a bug in the SQL generation?