首页 新闻 赞助 找找看

遍历List<T> 获取item的某个属性值 然后移除

0
[已关闭问题] 关闭于 2014-10-26 14:50

public static class ListRemove
{

//sortEx 是个传过来的那个属性 source需要比较的值
public static List<T> RemoveList<T>(this List<T> lists, string sortEx, string source)
{
foreach (var item in lists)
{

//如果item[sortEx] == source 请问大家 这一步该怎么写
//if (item.GetType(). == source)
//{
//lists.Remove(item);
//}
}
return lists;
}
}

crud的主页 crud | 初学一级 | 园豆:16
提问于:2013-04-08 14:17
< >
分享
所有回答(7)
0

if(item[sortEx].equal(source)){。。。。。。lists.Remove(。。。)。。。。。。。。。}

Beyond-bit | 园豆:2885 (老鸟四级) | 2013-04-08 14:30
-3

173844862.Net高级部落 欢迎你的加入!

辛巴 | 园豆:622 (小虾三级) | 2013-04-08 15:07
0

不能在foreach中移除项,会直接报错的

chenping2008 | 园豆:9836 (大侠五级) | 2013-04-08 15:55
0

经验:用list的iterator,进行操作。list的for循环遍历,增删操作有问题。

在大地画满窗子 | 园豆:102 (初学一级) | 2013-04-08 16:36
0

先将lists复制到另外一个LIST<T>中,然后循环复制的LIST<T>,若和你相同则移除lists中的item,然后返回lists;

tyler huang | 园豆:202 (菜鸟二级) | 2013-04-08 16:43
0

List<T> newList=new List<T>();

foreach (var item in lists)
{
  if (item.GetType(). == source)
  {
      newList.Add(item)l
  }

}
return newList;

不懂装懂 | 园豆:257 (菜鸟二级) | 2013-04-08 17:58
0
 //sortEx 是个传过来的那个属性 source需要比较的值
        public static List<T> RemoveList<T>(this List<T> lists, string sortEx, string source)
        {
            foreach (var item in lists)
            {
                if (item.GetType().GetProperty(sortEx).GetValue(item, null).ToString() == source)
                {
                    lists.Remove(item);
                    break;
                }
            }
            return lists;
        }

反射,学长只能帮你到这里了.

Dean@cnbolg | 园豆:210 (菜鸟二级) | 2013-04-09 16:47

@crud: 不要人云亦云好么 能不能让程序说话

支持(0) 反对(0) Dean@cnbolg | 园豆:210 (菜鸟二级) | 2013-04-13 14:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册