我有一个班。
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降序排列)。
我还试过这样的方法......还有这样的:......还有这样的:......(还是按字母顺序,而不是按Doc1Prcntg降序)。
// after the call to lstPairODocs.OrderByDescending():
List<PairODocs> lstPairODocs2 = lstPairODocs;
...还有这个
List<PairODocs> lstPairODocs2 = new List<PairODocs>(lstPairODocs);
...然后在循环中把lstPairODocs替换成lstPairODocs2:
foreach (PairODocs pod in lstPairODocs2)
...但是没有任何区别
Linq order by并不突变集合,而是返回一个新的集合,这个集合是有序的。只要抓住它。
YourList = YourList.OrderByDescending..
或者通过它进行迭代。
foreach(PairODocs p in YourList.OrderByDescending..)