私はをまず四半期でレコードをソートしようとしているコレクションを持っていますし、四半期の中で最高額であるをソートしようとしています。私はそれが最初にソートされあるOrderByの後にOrderByDescendingを実行するにはどうすればよいですか?
からOrderBy(o=>o.Quater).OrderByDescending(o=>o.Amount)
出力の変更を行う場合は
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; }
}
}
の下で出力が
であるように私のコードは、これまでのところです四半期別に、次に金額で計算します。
しかし、私はを探しています。クォーターと四半期内の最初の並べ替えは、金額 降順です。
所望の出力は、目標を達成してプログラムで変更する必要がある何
のですか?
事前に感謝します。
あなたはタイプミスがあります。クォーターではなくクォーターです。 –
2番目のOrderByをThenByに変更します。 google for "Enumerable ThenBy" https://msdn.microsoft.com/en-us/library/bb534743%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 –