在困难的项目中删除从列表? [英] Difficulty in Removing Items From List?

查看:201
本文介绍了在困难的项目中删除从列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表。首先是所有学生的,二是选定的学生的。我想,如果我一次选择的一些学生,他们将在所有学生的名单中删除。这里是我的代码,但它没有。学生将不会被删除。

 的foreach(在ListSelectedStudents.ToList VAR李())
{
如果(ListAllStudents.Contains(LI))
{
ListAllStudents.Remove(LI);
}
}


解决方案

有您尝试使用LINQ:

  ListAllStudents.RemoveAll(M = GT; ListSelectedStudents.Contains(M)); 

如果它不工作,也可能是一些错误的对象实现的默认比较,你既可以修复比较器,或者做类似:

  ListAllStudents.RemoveAll(M = GT; ListSelectedStudents.Any(N = > n.Id == m.Id)); //假设该ID是对象... 


的主键

I have two lists. The first is of all students and the second is of selected students. I want if I one time select some student they will remove from the all-student list. Here is my code but it doesn't. Students won't get removed.

foreach (var li in ListSelectedStudents.ToList())
{
    if (ListAllStudents.Contains(li))
    {
        ListAllStudents.Remove(li);
    }
}

解决方案

have you tried using linq:

ListAllStudents.RemoveAll(m => ListSelectedStudents.Contains(m));

if it does not work, it could be something wrong with the default comparison implemented in the object, and you could either fix the comparer, or do something like:

ListAllStudents.RemoveAll(m => ListSelectedStudents.Any(n=>n.Id == m.Id)); // Assume the Id is the primary key of the object...

这篇关于在困难的项目中删除从列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆