IEnumerable<Dictionary<string, object>> ListOfDic = new List<Dictionary<string, object>>()
{
new Dictionary<string, object> {{"Name", "Stud 1"}, {"Id", 111}},
new Dictionary<string, object> {{"Result", "Pass"}, {"Id", 111}},
new Dictionary<string, object> {{"Name", "Stud 2"}, {"Id", 222}},
new Dictionary<string, object> {{"Result", "Pass"}, {"Id", 222}},
new Dictionary<string, object> {{"Name", "Stud 3"}, {"Id", 333}},
new Dictionary<string, object> {{"Result", "Pass"}, {"Id", 333}},
new Dictionary<string, object> {{"Name", "Stud 4"}, {"Id", 444}},
new Dictionary<string, object> {{"Result", "Fail"}, {"Id", 444}},
new Dictionary<string, object> {{"Name", "Stud 5"}, {"Id", 555}},
new Dictionary<string, object> {{"Result", "Pass"}, {"Id", 555}},
new Dictionary<string, object> {{"Name", "Stud 6"}, {"Id", 666}},
new Dictionary<string, object> {{"Result", "Fail"}, {"Id", 666}},
new Dictionary<string, object> {{"Name", "Stud 7"}, {"Id", 777}},
new Dictionary<string, object> {{"Result", "Fail"}, {"Id", 777}},
new Dictionary<string, object> {{"Name", "Stud 8"}, {"Id", 888}},
new Dictionary<string, object> {{"Result", "Fail"}, {"Id", 888}},
new Dictionary<string, object> {{"Name", "Stud 9"}, {"Id", 999}},
new Dictionary<string, object> {{"Result", "Pass"}, {"Id", 999}}
};
List<int> StudentsToRemove = new List<int>() {555, 777, 888};
我想删除其id为555,777,888的词典中的所有元素。我尝试了以下解决方案,但它无效,在我的项目中, ListOfDic
中有超过7000个元素。所以我认为最好将其转换为ToList()
,以便RemoveAll()
可以一次删除元素。如果有任何其他更好的解决方案,请告诉我。
//Tried Solution, Not Working !! :(
foreach (var stud in StudentsToRemove)
{
ListOfDic.ToList().RemoveAll(a => a.ContainsValue(stud));
}
答案 0 :(得分:1)
您说ToList
无效,但可能 ;只是ToList
创建了一个新集合,而RemoveAll
不会影响您的原始集合。更糟糕的是,您正在循环的每次迭代中创建此列表 。
因此,为了解决您的问题,您可以存储新的列表(并允许旧的列表进行垃圾收集):
List<Dictionary<string, object>> correctList = ListOfDic.ToList();
foreach (var stud in StudentsToRemove)
{
correctList.RemoveAll(a => a.ContainsValue(stud));
}
在您的方法中使用correctList
代替ListOfDict
。
答案 1 :(得分:1)
你应该尝试这样的事情:
const { request, user } = param
您可以使用上面的代码将过滤后的集合分配给实际对象,而不是删除项目。这将检查字典中ListOfDic = ListOfDic.Where(x => x.Any(y => y.Key == "Id" &&
!StudentsToRemove.Contains((int)y.Value))).ToList();
为key
且Id
答案 2 :(得分:0)
您对问题有答案,但我的解决方案是帮助您了解编码并使用最佳做法。
Dictionary类本身就是一个集合,因此将字典单例粘贴到列表中是错误的编码。我建议创建一个<Student>
类,至少有两个属性Name和Result。然后使用字典<int,Student>
,您可以为每个人创建一个条目。
class Student
{
public string Name {get; set;}
public string Result {get; set;}
public Person(string name, string result)
{
this.Name =name;
this.Result = result;
}
}
Dictionary<int,Student> students = new Dictionary<int,Student>() {};
students[111] = new Student(){ "Stud 1", "Pass"};
students[222] = new Student(){ "Stud 2", "Pass"};
students[333] = new Student(){ "Stud 3", "Pass"};
students[444] = new Student(){ "Stud 4", "Fail"};
students[555] = new Student(){ "Stud 5", "Pass"};
students[666] = new Student(){ "Stud 6", "Fail"};
students[777] = new Student(){ "Stud 7", "Fail"};
students[888] = new Student(){ "Stud 8", "Fail"};
students[999] = new Student(){ "Stud 9", "Pass"};
然后,您可以从StudentsToRemove列表中删除所需的所有学生。制作动态(在运行时)列表以删除所有失败学生的示例:
foreach(int x in students.Where(x => x.Value.Result == "Fail").Select(x => x.Key).ToList() )
{
students.Remove(x);
}
只是为了测试你的输出,看你有你想要的东西你可以使用它:
foreach(var x in students)
{
Console.WriteLine("key:{0} Student name:{1} result:{2}",x.Key, x.Value.Name, x.Value.Result);
}