【问题标题】:Select latest two row of chat history linq选择最新的两行聊天记录 linq
【发布时间】:2020-08-19 08:00:51
【问题描述】:

我有一个 ChatMessage 表,我想选择用户聊天历史记录作为他/她与他人或他自己的最新消息列表,例如什么应用程序。

当您打开 whats 应用程序时,您会看到一个显示最新聊天记录的聊天记录列表。

表架构是这样的:

SenderId    ReciverId    Message
--------------------------------
1           2            hello
1           2            how are you?
2           1            hey
2           1            i'm fine
2           2            for myself
2           3            are you there?

我试过这个查询:

SELECT MAX(SenderID), ReciverID, Message
FROM ChatMessages
WHERE SenderID = 2 OR ReciverID = 2
GROUP BY SenderID, ReciverID order by SenderID

这个查询的结果是:

    senderId    ReciverId    Message
    --------------------------------
    1           2            how are you?
    2           1            i'm fine
    2           2            for myself
    2           3            are you there?

第 1 行和第 2 行必须合并并只显示:2 1 i'm fine

我正在寻找的结果是:

    senderId    ReciverId    Message
    --------------------------------
    2           1            i'm fine
    2           2            for myself
    2           3            are you there?

解决办法是什么?如果可能的话,在 linq 或 lambda 中?

【问题讨论】:

  • 您的示例中没有 linq 代码,只有 sql。只是想知道,如何在没有消息日期时间的情况下获得最新的聊天?
  • 我知道这是 sql,我询问了这个查询的 linq。实际上我有 datetime 字段,但你可以看到我使用 GroupBy Max 所以只有一个 Max id 会返回。问题是结果有重复的最新消息行(在本例中为第 1 行和第 2 行)。
  • @MohammadRezaMrg 上面显示的结果是你想要的吗?
  • 不清楚你想要什么。您还应该在问题中显示相关字段,例如消息的日期时间
  • 问题已更新以显示我正在寻找的结果。

标签: c# linq .net-core lambda entity-framework-core


【解决方案1】:

我通过使用 groupBy 得到这个并获取每个合作伙伴的 2 条最新消息 然后使用 distinctby(from morelinq) 删除重复项。

string uid =  "a90566ab-eef7-4f57-8e96-a3b7cc8ce786";               

var chatIdsQuery = _context.ChatMessages
    .Where(o => o.SenderID.Equals(uid) || o.ReciverID.Equals(uid))                    
    .Select(x => new
    {
         x.Id,
        x.ReciverID,
        x.SenderID
    })                     
    .GroupBy(g => new { g.SenderID, g.ReciverID } )
    .Select(k => new {
        Id = k.Max(x => x.Id),
        PartnerId = k.Key.SenderID.Equals(uid) ? k.Key.ReciverID : k.Key.SenderID,
        //CreateTime = k.Max(x => x.CreateTime)
    })
    .OrderByDescending(d => d.Id)                  
    .AsQueryable(),
    .DistinctBy(i => i.PartnerId);  // <============= just remove dublicate

var chatListQuery = from l in chatIdsQuery
               join x in _context.ChatMessages on l.Id equals x.Id
               orderby x.CreateTime descending
               select (new
               {
                   x.Id,
                   l.PartnerId,
                   x.Text,
                   x.CreateTime
               });
var chatList = chatListQuery.ToList();

对于异步:

var chatList = await chatListQuery.ToListAsync();
chatList = chatList.DistinctBy(i => i.PartnerId).ToList();

或:

chatList = chatList.GroupBy(x => x.PartnerId).Select(x => x.First()).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2020-05-29
    相关资源
    最近更新 更多