I have case which I have a set of users some are bot and some are not. I also use pagination with a size of 9. I want to take 5 non bot users and 4 bot users in a page. I am listing users with this lines of code.
var list = await _userRepository
.ListGeneric(filterBy: finalExpression,
orderBy: r => r.CreatedAt,
thenBy: r => String.IsNullOrEmpty(r.PP) == false &&
r.PP != "somestringvalue" &&
r.PP != "somestringvalue",
skip: skip,
limit: paginationQuary.PageSize,
cancellationToken);
At user repository ListGeneric looks like this.
public Task<List<User>> ListGeneric<TOrder, TThen>(Expression<Func<User, bool>> filterBy, Expression<Func<User, TOrder>> orderBy, Expression<Func<User, TThen>> thenBy, int skip, int limit, CancellationToken cancellationToken = default)
{
return GetQuery(filterBy)
.OrderByDescending(thenBy)
.ThenByDescending(orderBy)
.Skip(skip)
.Take(limit)
.ToListAsync(cancellationToken: cancellationToken);
}
And at last User model is like this
public class User : DbBase
{
public string Name { get; set; }
public string Surname { get; set; }
public string SearchName { get; set; }
public string Email { get; set; }
public string MobilePhoneCode { get; set; }
public string MobilePhone { get; set; }
public string PP { get; set; }
public bool IsBot { get; set; }
}
I think joining IsBot ones and not IsBot ones seperately but is there a better approach in this case? Much appreciated for the help.
Don't know if this is the cleanest way or not, but if you want a solution that's extremely transparent you can simply add a property to your User
class that has the sort order (or sort page) and then do a simple loop to set it before rendering. It would be at worst O(n).
So for example add this property to your class:
public int SortOrder { get; set; }
And then before you render:
int botCount = 0;
int humanCount = 0;
foreach (User u in <your filtered, sorted IEnumerable>)
{
u.SortOrder = u.IsBot ? botCount++ / botsPerPage : humanCount++ / humansPerPage;
}
Your display would then sort by this value first. I took a list of 11 humans and 10 bots and this was the result:
Hambone1 : False => 0
Hambone2 : False => 0
Hambone3 : False => 0
Hambone4 : False => 0
Hambone5 : False => 0
Hambone6 : False => 1
Hambone7 : False => 1
Hambone8 : False => 1
Hambone9 : False => 1
Hambone10 : False => 1
Hambone11 : False => 2
bot1 : True => 0
bot2 : True => 0
bot3 : True => 0
bot4 : True => 0
bot5 : True => 1
bot6 : True => 1
bot7 : True => 1
bot8 : True => 1
bot9 : True => 2
bot10 : True => 2
So you can see given 5 humans and 4 bots per page, even though the bots were at the very end page (sort) = 1 had Hambone 1-5 and bot 1-4.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.