我一直在这里玩弄一个非常基本的东西。所以我在这里有一个类:
public class LinqItem
{
public int ID { get; set; }
public int AccountNumber { get; set; }
public double Value { get; set; }
public int Year { get; set; }
public int Gifts { get; set; }
public int FirstYear { get; set; }
public double LargestYear { get; set; }
public int MonthGiving { get { return Gifts > 5 ? Gifts : 0; } }
}
我有一个返回LinqItem类型集合的代码。这是代码。
List<LinqItem> items = LoadCSV();
//some code to produce my collection. NO problem till this point, so I don't think LoadCSV function is required at all.
List<LinqItem> newItems = items
.OrderByDescending(c => c.LargestYear)
.ThenBy(c => c.AccountNumber)
.ThenBy(c => c.Year).ToList();
现在,在上面一行中,我要做的是首先按LargestYear Descending排序集合,然后按帐号Ascending排序,然后按Year Ascending排序。
排序后,我正在创建一个包含输出的CSV文件。整个函数看起来像这样:
private void CreateCSV(string csvFile)
{
List<LinqItem> items = LoadCSV();
List<LinqItem> newItems = items.OrderByDescending(c => c.LargestYear).ThenBy(c => c.AccountNumber).ThenBy(c => c.Year).ToList();
StringBuilder text = new StringBuilder();
int index = 1;
foreach (LinqItem i in newItems)
{
text.AppendLine(i.AccountNumber + "," + i.Year + "," + i.Value + "," + i.Gifts + "," + index + "," + i.FirstYear + "," + i.LargestYear);
index++;
}
StreamWriter w = new StreamWriter(csvFile);
w.Write(text.ToString());
w.Close();
}
现在,到目前为止,非常简单。但是在CSV中的问题,不同帐号的最大年份值是相同的。理想情况下,我希望如果最大年份相同,那么这些项目将根据帐号和年份进行排序。但它没有发生。请参阅以下屏幕截图:
在屏幕截图中,A列是帐号,B列是年,G列是最大年份。在理想的世界中,根据我的排序假设,我假设101864(单元格A6303)应该在103816(单元格A6297)之前,因为我将帐号排序为升序。有没有人有任何想法?