使用LINQ创建了一个排序字典,不能删除条目

本文关键字:字典 排序 不能 删除 一个 创建 LINQ 使用 | 更新日期: 2023-09-27 18:04:28

我使用LINQ创建了一个基于另一个字典的排序字典。下面是我使用的代码:

var sortedStudentRatioDict = from entry in studentRatioDict orderby entry.Value ascending select entry;

现在我不能从字典中删除任何条目。.Remove()方法在智能感知中没有显示,也没有工作。如何从这个字典中删除条目,或者如何按值对字典排序以便删除条目?非常感谢您的帮助

使用LINQ创建了一个排序字典,不能删除条目

正如pero所说,它不是Dictionary,它是IEnumerable<KeyValuePair<TKey, TValue>>。您可以通过调用ToDictionary()方法将其转换为Dictionary,如下所示:

var sortedStudentRatioDict = (from entry in studentRatioDict orderby entry.Value ascending select entry).ToDictionary(_ => _.Key, __ => __.Value);