代码之家  ›  专栏  ›  技术社区  ›  Rana Mujahid

orderbydescing linq方法asp.net core 2.0中作为参数的对象列表

  •  0
  • Rana Mujahid  · 技术社区  · 7 年前

    我有两个模态类:

    1. 对话

      public long Id 
      public string SenderId
      public string RecieverId
      public virtual List<Message> Messages
      public virtual User User
      
    2. 信息

      public long Id
      public string MessageBody
      public long ConversationId
      public string SenderId 
      public DateTime CreatedDate
      

    我想和你谈谈 OrderbyDescending 关于消息 CreatedDate .

    我的意思是按创建日期在顶部对最后一条消息是最新的会话进行排序。比如:

    var conversations = _context.Conversations
        .Include(x => x.Messages)
        .Include(x => x.Users)
        .Where(x => x.RecieverId== userId)
        .OrderByDescending(x => x.Messages.Any(x => x.CreatedDate))
        .ToList();
    

    但是这样我就写不出来了 OrderByDescending 声明。

    如果可以的话,请给我一些建议。

    希望有人能理解我的观点。

    1 回复  |  直到 7 年前
        1
  •  0
  •   greyhame    7 年前

    您应该按照对话中的“最新”消息进行排序,因此您需要按照每条消息中的“最高”日期进行排序,这是使用LINQ实现的。 Max() 方法。 看看它是否能帮助你:

     .OrderByDescending(conversation => conversation.Messages.Max(message => message.CreatedDate)).ToList();