【问题标题】:EFCore + Linq, how to Select specify users first, then rest of them?EF Core + Linq,如何先选择特定用户,然后再选择其余用户?
【发布时间】:2021-09-14 10:57:44
【问题描述】:

我有当前在线的用户列表:

List<int> onlineUsers = _connectedUsersEntity.GetOnlineIds();

然后我提出请求:

var profiles = await _cnx.Users
                .OrderBy(p => onlineUsers.Any(z => z == p.Id))
                .ThenBy(p => p.Id)
                .Select(p => new { p.Id, p.DisplayName, p.ProfilePhoto })
                .Skip(request.Skip)
                .Take(request.Take)
                .ToListAsync();

我想拥有:

//skip 0 take 3
{"user1":"online"}
{"user3":"online"}
{"user2":"offline"}

//then next call skip 3, take 3
{"user4":"offline"}
{"user5":"offline"}
{"user6":"offline"}

我有什么:

{"user1":"online"}
{"user2":"offline"}
{"user3":"online"}

如何为此做出正确的 linq 语句?

【问题讨论】:

  • 我在一个项目中实现了PaginatedList。我会检查一下,看看它是否适合你。

标签: .net linq entity-framework-core


【解决方案1】:

如果我理解正确的话:

var profiles = await _cnx.Users
    .OrderBy(p => onlineUsers.Contains(p.Id) ? 0 : 1)
    .ThenBy(p => p.Id)
    .Skip(request.Skip)
    .Take(request.Take)
    .Select(p => new { p.Id, p.DisplayName, p.ProfilePhoto })
    .ToListAsync();

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 2022-12-06
    • 1970-01-01
    • 2020-07-22
    • 2019-11-26
    • 2021-06-18
    • 2019-12-30
    • 2018-08-02
    • 1970-01-01
    相关资源
    最近更新 更多