如何执行OrderBy后跟OrderByDescending?

pri*_*kar -1 c# linq sorting lambda

我有一个集合,我试图按季度排序记录,然后在季度内按最高金额排序.到目前为止我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Test> lstTest = new List<Test>();
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 2), amount = 2500 });
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 2), amount = 10000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 5), amount = 4000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 10), amount = 40000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 15), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 1, 25), amount = 12000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 2, 5), amount = 38000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 2, 10), amount = 38000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 2, 15), amount = 4000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 2, 20), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 2, 20), amount = 20000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 3, 15), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 3, 20), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 3, 20), amount = 4000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 3, 31), amount = 1000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 4, 9), amount = 50000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 4, 11), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 4, 21), amount = 1000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 4, 21), amount = 10000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 4, 28), amount = 5000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 5, 5), amount = 45000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 5, 7), amount = 98000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 5, 9), amount = 7000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 5, 25), amount = 2000 });
            lstTest.Add(new Test { dt = new DateTime(2017, 5, 31), amount = 1000 });

            var result = lstTest.Select(x => new
            {
                Amount = x.amount,
                Date = x.dt,
                MonthDiff = GetMonthsDiff(DateTime.Now, x.dt),
                Quater = GetQuarter(DateTime.Now, x.dt)
            }).OrderBy(o=>o.Quater).ToList();

            foreach (var res in result)
            {
                Console.WriteLine("Amount = {0}  Date= {1} MonthDiff= {2}  Quater= {3}", res.Amount, res.Date, res.MonthDiff, res.Quater);
            }
            Console.ReadKey();
        }


        public static string GetQuarter(DateTime start, DateTime end)// int month)
        {
            int month = GetMonthsDiff(start, end);
            string quarter =  month <= 3 ? "Q1" : (month >= 4 && month <= 6) ? "Q2" : (month >= 7 && month <= 9) ? "Q3" : "Q4";
            return quarter;
        }



        public static int GetMonthsDiff(DateTime start, DateTime end)
        {
            if (start > end)
                return GetMonthsDiff(end, start);

            int months = 0;
            do
            {
                start = start.AddMonths(1);
                if (start > end)
                    return months;

                months++;
            }
            while (true);
        }
    }

    public class Test
    {
        public DateTime dt { get; set; }
        public int amount { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是

在此输入图像描述

如果我OrderBy(o=>o.Quater).OrderByDescending(o=>o.Amount)将输出更改为

在此输入图像描述

这是它首先按季度排序,然后按金额排序.

但我正在寻找按季度排序的第一种,并按季度排序按数量下降.

期望的输出是

在此输入图像描述

需要在程序中修改哪些才能实现目标?

fub*_*ubo 10

更换

OrderBy(o=>o.Quater).OrderByDescending(o=>o.Amount)
Run Code Online (Sandbox Code Playgroud)

OrderBy(o=>o.Quater).ThenByDescending(o=>o.Amount)
Run Code Online (Sandbox Code Playgroud)

ThenByDescending通过使用指定的比较器以降序执行序列中元素的后续排序.