【问题标题】:How to sort data based on CreatedUtc Date using mongodb query in c#?如何在 C# 中使用 mongodb 查询根据 CreatedUtc Date 对数据进行排序?
【发布时间】:2020-01-22 04:46:38
【问题描述】:

我想根据 CreatedUtc 时间对数据进行排序。我曾尝试使用 Reverse 功能,它似乎可以解决,但仍在寻找一些替代选项。

var result = _participantRepo.AsQueryable().Where(x => x.Id == ParticipantId).SelectMany(x => 
    x.Relations).ToList().Where(x => x.UserId != AppUserId).Select(r => new RelationVM
                    {
                        IsOwner = r.UserId == participant.CreatedByUserId,
                        FirstName = r.FirstName,
                        LastName = r.LastName,
                        Email = r.Email,
                        UserId = r.UserId,
                        RelationType = r.RelationType,
                        Role = r.Role,
                        IsAccepted = r.IsAccepted,
                        AvatarUrl = r.AvatarUrl,
                        CreatedUtc = r.CreatedUtc
                    }).Reverse().ToList();

【问题讨论】:

    标签: c# mongodb-query


    【解决方案1】:

    您需要关注两件事:

    因此,您的查询应如下所示

        var result = _participantRepo.AsQueryable().Where(x => x.Id == ParticipantId).SelectMany(x => 
            x.Relations).Where(x => x.UserId != AppUserId).Select(r => new RelationVM
                            {
                                IsOwner = r.UserId == participant.CreatedByUserId,
                                FirstName = r.FirstName,
                                LastName = r.LastName,
                                Email = r.Email,
                                UserId = r.UserId,
                                RelationType = r.RelationType,
                                Role = r.Role,
                                IsAccepted = r.IsAccepted,
                                AvatarUrl = r.AvatarUrl,
                                CreatedUtc = r.CreatedUtc
                            }).Reverse().OrderBy(g => g.CreatedUtc).ToList();
    

    【讨论】:

      【解决方案2】:

      .OrderBy(g => g.CreatedUtc) 怎么样?

      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2017-11-23
      • 2017-11-04
      相关资源
      最近更新 更多