【发布时间】:2020-06-11 22:34:33
【问题描述】:
我有一堂课:
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)
...但这并没有什么不同。
【问题讨论】:
-
它返回一个新值。不能就地操作。 Documentation with examples is available online
-
我遇到了这个问题,有效的是创建列表,并像这样创建该列表的新变量,
var newList = 1stPairODocs2.OrderByDescending(e => e.whatever);这并不理想,但这就是我让它工作的方式 -
众所周知,在 C# 中绝对没有任何作用 - 你可以自己看到它 bing.com/search?q=c%23+OrderByDescending+does+not+work ...(即使是像 String.Replace does not work 这样的简单操作......你对排序的期望): )