使用 OrderBy 排序 collection 无效
Sorting collection using OrderBy not working
我正在尝试根据(在本例中为“金额”)使用 lambda 对我的 collection 进行排序,但也尝试了其他字段。当我使用 foreach 输出 collection 时,它没有根据字段
排序
Accounts? ac = await Client.SendRequest<Accounts>(tk.access_token, Method.GET, "/UploadedAccounts", "");
ac.value.OrderBy(x => x.Amount);
ac.value.RemoveAll(x => x.Year.ToString() != YearBox.Text);
foreach (var a in ac.value) {
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
我也试过在 'RemoveAll' 之后进行排序
我做错了什么?
.OrderBy(x => x.Amount)
对集合进行排序,return 对集合进行排序 Accounts
。您需要在 .OrderBy(x => x.Amount)
的 return 列表中执行 foreach
喜欢,
foreach (var a in ac.value.OrderBy(x => x.Amount))
{
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
问题是 OrderBy
LINQ 扩展正在返回已排序的可枚举。它没有修改原始集合。
我正在尝试根据(在本例中为“金额”)使用 lambda 对我的 collection 进行排序,但也尝试了其他字段。当我使用 foreach 输出 collection 时,它没有根据字段
排序Accounts? ac = await Client.SendRequest<Accounts>(tk.access_token, Method.GET, "/UploadedAccounts", "");
ac.value.OrderBy(x => x.Amount);
ac.value.RemoveAll(x => x.Year.ToString() != YearBox.Text);
foreach (var a in ac.value) {
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
我也试过在 'RemoveAll' 之后进行排序 我做错了什么?
.OrderBy(x => x.Amount)
对集合进行排序,return 对集合进行排序 Accounts
。您需要在 .OrderBy(x => x.Amount)
foreach
喜欢,
foreach (var a in ac.value.OrderBy(x => x.Amount))
{
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
问题是 OrderBy
LINQ 扩展正在返回已排序的可枚举。它没有修改原始集合。