为什么我的有序列表不保持有序?
Why does my ordered list not stay ordered?
我有一个 class:
public class PairODocs
{
public string Whirred;
public int Doc1Count = 0;
public double Doc1Prcntg = 0.0;
public int Doc2Count = 0;
public double Doc2Prcntg = 0.0;
}
...及其列表:
List<PairODocs> lstPairODocs;
当我尝试通过 Doc1Prcntg 订购时,像这样:
lstPairODocs.OrderByDescending(a => a.Doc1Prcntg);
...它显然没有这样做。然后使用下面的循环代码将其添加到 PDF 文档 (iText 7):
foreach (PairODocs pod in lstPairODocs)
{
table.AddCell(pod.Whirred);
table.AddCell(pod.Doc1Count.ToString());
table.AddCell(pod.Doc1Prcntg.ToString());
table.AddCell(pod.Doc2Count.ToString());
table.AddCell(pod.Doc2Prcntg.ToString());
}
...这是生成的数据:
(仍按字母顺序排列,而不是按 Doc1Prcntg 降序排列)。
我也试过这个:
// after the call to lstPairODocs.OrderByDescending():
List<PairODocs> lstPairODocs2 = lstPairODocs;
...还有这个:
List<PairODocs> lstPairODocs2 = new List<PairODocs>(lstPairODocs);
...然后在循环中用 lstPairODocs2 替换 lstPairODocs:
foreach (PairODocs pod in lstPairODocs2)
...但这没有区别。
Linq order by 不会改变集合,它returns 是一个新的有序集合。抓住它:
YourList = YourList.OrderByDescending..
或遍历它:
foreach(PairODocs p in YourList.OrderByDescending..)
我有一个 class:
public class PairODocs
{
public string Whirred;
public int Doc1Count = 0;
public double Doc1Prcntg = 0.0;
public int Doc2Count = 0;
public double Doc2Prcntg = 0.0;
}
...及其列表:
List<PairODocs> lstPairODocs;
当我尝试通过 Doc1Prcntg 订购时,像这样:
lstPairODocs.OrderByDescending(a => a.Doc1Prcntg);
...它显然没有这样做。然后使用下面的循环代码将其添加到 PDF 文档 (iText 7):
foreach (PairODocs pod in lstPairODocs)
{
table.AddCell(pod.Whirred);
table.AddCell(pod.Doc1Count.ToString());
table.AddCell(pod.Doc1Prcntg.ToString());
table.AddCell(pod.Doc2Count.ToString());
table.AddCell(pod.Doc2Prcntg.ToString());
}
...这是生成的数据:
(仍按字母顺序排列,而不是按 Doc1Prcntg 降序排列)。
我也试过这个:
// after the call to lstPairODocs.OrderByDescending():
List<PairODocs> lstPairODocs2 = lstPairODocs;
...还有这个:
List<PairODocs> lstPairODocs2 = new List<PairODocs>(lstPairODocs);
...然后在循环中用 lstPairODocs2 替换 lstPairODocs:
foreach (PairODocs pod in lstPairODocs2)
...但这没有区别。
Linq order by 不会改变集合,它returns 是一个新的有序集合。抓住它:
YourList = YourList.OrderByDescending..
或遍历它:
foreach(PairODocs p in YourList.OrderByDescending..)