Remove object from a collection

J

Jibesh

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<ParametermappingObj> myList = 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
 
J

Jibesh

Thanks Pete.

sorry i miss typed it.

Its RemoveAll and not Remove. Remove only accepts reference and not
Predicate as you said.


Regards,
Jibesh
 
T

Tony Johansson

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
 
T

Tony Johansson

Hello!

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

//Tony
 
G

Göran Andersson

Tony said:
Hello!

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

//Tony

No, there is no other way to locate an item in the list other than
looping through the items in the list.

You can use the RemoveAll method to remove specific items:

theMappingList.RemoveAll(delegate(ParametermappingObj p){ return p.Name
== "CC" && p.Id == "123-CHISI"; });

However, if you know that there is only one item to remove, it's more
efficient to do the looping yourself, so that you can exit from the loop
as soon as you have found the item to remove.
 
I

Ignacio Machin ( .NET/ C# MVP )

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

No, you have to iterate on the collection but you can do it in an
elegant way :)
List<ParametermappingObj> list = new .....
......
ParametermappingObj element = null;
element = list.Find( x=>x.Name == "CC" && x.Id="123-CHISI");
if (element!= null)
list.Remove(element);
 
I

Ignacio Machin ( .NET/ C# MVP )

No, there is no other way to locate an item in the list other than
looping through the items in the list.

You can use the RemoveAll method to remove specific items:

theMappingList.RemoveAll(delegate(ParametermappingObj p){ return p.Name
== "CC" && p.Id == "123-CHISI"; });

However, if you know that there is only one item to remove, it's more
efficient to do the looping yourself, so that you can exit from the loop
as soon as you have found the item to remove.

You can use LINQ in a very elegant way. it will iterate until the
element is found for the first time.
 
I

Ignacio Machin ( .NET/ C# MVP )

In what way is that an improvement over calling Find() and then Remove()?
And is it as efficient as doing the enumeration explicitly, keeping a
counter, and then removing the element by index?

More code?
Other than that there should be no difference, there is no other way
than iterate until find it, break the loop and then remove it. As I
said there is no other way around it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top