从集合中删除对象 [英] Remove object from a collection

查看:67
本文介绍了从集合中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我有一个包含大量对象的集合。

每个对象都有以下定义。

public class ParametermappingObj

{

private string Name {get; set;}

private string Id {get; set;}

这里定义的其他一些字段

....

}


如果我知道例如想删除具有以下键值的对象

Name =" CC"和Id =" 123-CHISI"怎么做的?


有没有更好的方法来循环整个集合


// Tony

Hello!

I have a collection with a lot of object.
Each object has the following definition.
public class ParametermappingObj
{
private string Name {get;set;}
private string Id {get;set;}
some other field defined here
....
}

If I know for example want to remove object with the following key values
Name = "CC" and Id = "123-CHISI" how is that done ?

Is there a better way then to loop through the whole collection

//Tony

推荐答案

你好!


我忘了说我使用的是VS2008而且这个系列包含一个通用的

List< ParametermappingObj>


// Tony

" Tony Johansson" < t。********* @ logica.comwrote in message

news:ez ************** @ TK2MSFTNGP03.phx.gbl ...
Hello!

I forgot to say that I use VS2008 and the collection consist of a generic
List<ParametermappingObj>

//Tony
"Tony Johansson" <t.*********@logica.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...

您好!


我有一个包含大量对象的集合。

每个对象都有以下定义。

公共类ParametermappingObj

{

private string Name {get; set;}

私人字符串Id {get; set;}

此处定义的其他字段

...

}


如果我知道例如想要删除具有以下键值的对象

Name =" CC"和Id =" 123-CHISI"怎么做的?


有没有更好的方法来循环整个集合

// Tony
Hello!

I have a collection with a lot of object.
Each object has the following definition.
public class ParametermappingObj
{
private string Name {get;set;}
private string Id {get;set;}
some other field defined here
...
}

If I know for example want to remove object with the following key values
Name = "CC" and Id = "123-CHISI" how is that done ?

Is there a better way then to loop through the whole collection
//Tony


Tony Johansson写道:
Tony Johansson wrote:

你好!


我忘了说我使用VS2008和该系列包含一个

泛型列表< ParametermappingObj>


// Tony


" Tony约翰森" < t。********* @ logica.comwrote in message

news:ez ************** @ TK2MSFTNGP03.phx.gbl ...
Hello!

I forgot to say that I use VS2008 and the collection consist of a
generic List<ParametermappingObj>

//Tony
"Tony Johansson" <t.*********@logica.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...

>您好!

我有一个包含大量对象的集合。
每个对象都有以下内容定义。
公共类ParametermappingObj
{private private Name {get; set;}
私有字符串Id {get; set;}
此处定义的其他字段
...

如果我知道例如想要删除具有以下键值的对象
Name =" CC"和Id =" 123-CHISI"怎么做?

有没有更好的方法来循环整个集合
// Tony
>Hello!

I have a collection with a lot of object.
Each object has the following definition.
public class ParametermappingObj
{
private string Name {get;set;}
private string Id {get;set;}
some other field defined here
...
}

If I know for example want to remove object with the following key values
Name = "CC" and Id = "123-CHISI" how is that done ?

Is there a better way then to loop through the whole collection
//Tony



不,除了

循环浏览列表中的项目之外,没有其他方法可以在列表中找到项目。


您可以使用RemoveAll方法删除特定项目:

theMappingList.RemoveAll(delegate(Parametermapping Obj p){return p.Name

==" CC"& & p.Id ==" 123-CHISI";});


但是,如果你知道只有一个要删除的项目,那就更多了/>
自己进行循环有效,这样一旦找到要移除的项目,就可以退出循环




-

G?跑Andersson

_____
http://www.guffa.com


Tony,


您还可以尝试L的谓词方法ist< T>。删除(...),此方法还可以通过列表元素搜索
。阅读MSDN了解更多详情。


例如:

公共类ParametermappingObj

{

private string _name;

public ParametermappingObj(string name)

{

_name = name;

}

公共字符串名称

{

get {return this._name; }

set {this._name = value; }

}

}


公共部分类MyClass


{


静态字符串id =" aa" ;;


public MyClass()


{


列表< ParametermappingObjmyList = new List< ParametermappingObj>();


ParametermappingObj aa = new ParametermappingObj(" aa");


ParametermappingObj bb = new ParametermappingObj(" bb");


myList.Add(aa);


myList.Add(bb);


ParametermappingObj sublist = myList.Find(FindId); // myList.Remove(FindId)


}


public static bool FindId(ParametermappingObj obj){


if(obj.Name == id){return true; }


返回false;


}


}


谢谢

Jibesh

" G?ran Andersson" < gu *** @ guffa.com写信息

新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
Tony,

You can also try Predicate method of List<T>.Remove(...), This method also
searching through the list elements. Read MSDN for more details.

eg:
public class ParametermappingObj
{
private string _name;
public ParametermappingObj(string name)
{
_name = name;
}
public string Name
{
get { return this._name; }
set { this._name = value; }
}
}

public partial class MyClass

{

static string id = "aa";

public MyClass()

{

List<ParametermappingObjmyList = new List<ParametermappingObj>();

ParametermappingObj aa = new ParametermappingObj("aa");

ParametermappingObj bb = new ParametermappingObj("bb");

myList.Add(aa);

myList.Add(bb);

ParametermappingObj sublist = myList.Find(FindId); // myList.Remove(FindId)

}

public static bool FindId(ParametermappingObj obj){

if (obj.Name == id){ return true; }

return false;

}

}

Thanks
Jibesh
"G?ran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

Tony Johansson写道:
Tony Johansson wrote:

>你好!

我忘记了说我使用VS2008并且该集合包含一个通用的
List< ParametermappingObj>

// Tony

" Tony Johansson" < t。********* @logica.com写了留言
新闻:ez ************** @ TK2MSFTNGP03.phx.gbl ...
>Hello!

I forgot to say that I use VS2008 and the collection consist of a generic
List<ParametermappingObj>

//Tony
"Tony Johansson" <t.*********@logica.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...

>>您好!

我有一个包含大量对象的集合。
每个对象都有以下定义。
公共类ParametermappingObj
{private private Name {get; set;}
私有字符串Id {get; set;}
此处定义的其他字段
...


如果我知道例如想用以下键删除对象

Name =" CC"和Id =" 123-CHISI"怎么做?

有没有更好的方法来循环整个集合
// Tony
>>Hello!

I have a collection with a lot of object.
Each object has the following definition.
public class ParametermappingObj
{
private string Name {get;set;}
private string Id {get;set;}
some other field defined here
...
}

If I know for example want to remove object with the following key
values
Name = "CC" and Id = "123-CHISI" how is that done ?

Is there a better way then to loop through the whole collection
//Tony



不,除了通过列表中的项目循环

之外,没有其他方法可以在列表中找到项目。


您可以使用RemoveAll方法删除特定项目:

theMappingList.RemoveAll(delegate(Parametermapping Obj p){return p.Name ==

" CC"& & p.Id ==" 123-CHISI";});


但是,如果你知道只有一个要删除的项目,那就更多了/>
自己进行循环有效,这样一旦找到要移除的项目,就可以退出循环




-

G?跑Andersson

_____
http://www.guffa.com



这篇关于从集合中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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