Lambda表达式中的OrderByDescending?

时间:2014-04-30 13:51:01

标签: linq

以下是我想要按日期值获取订单的代码。任何人都可以帮我解决问题吗?

pointsCore.Categories
          .Where(d => d.EventCount != WebUi.NOCOUNT)
          .SelectMany(c => c.Events)
          .Select( x => Convert.ToDateTime(x.EventDate)
                               .ToString("MMM"))
          .Distinct()
          .ToList();

3 个答案:

答案 0 :(得分:3)

当然 - 只需在之前添加,然后转换为字符串月份名称:

pointsCore.Categories
          .Where(d => d.EventCount != WebUi.NOCOUNT)
          .SelectMany(c => c.Events)
          .OrderByDescending(x => x.EventDate)     // <-----------
          .Select( x => Convert.ToDateTime(x.EventDate)
                               .ToString("MMM"))
          .Distinct()
          .ToList();

答案 1 :(得分:1)

您只需在OrderByDescending上添加LINQ EventDate

pointsCore.Categories.Where(d => d.EventCount != WebUi.NOCOUNT)
          .SelectMany(c => c.Events)
          .OrderByDescending(d => d.EventDate)
          .Select(x => Convert.ToDateTime(x.EventDate).ToString("MMM"))
          .Distinct()
          .ToList();

答案 2 :(得分:0)

您希望在转换为字符串格式之前进行排序。我也会在排序和格式化之前做区分:

pointsCore.Categories
        .Where(d => d.EventCount != WebUi.NOCOUNT)
        .SelectMany(c => c.Events)
        .Distinct()
        .OrderByDescending(ev => ev.EventDate)
        .Select( x => Convert.ToDateTime(x.EventDate)
                           .ToString("MMM"))         
        .ToList();