473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Remove object from a collection

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
Oct 3 '08 #1
10 2385
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...
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
Oct 3 '08 #2
Tony Johansson wrote:
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...
>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, 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(Parametermapping Obj 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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 3 '08 #3
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 wrote:
>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...
>>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, 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(Parametermapping Obj 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.

--
Göran Andersson
_____
http://www.guffa.com

Oct 3 '08 #4
On Oct 3, 10:04 am, "Tony Johansson" <t.johans...@logica.comwrote:
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<ParametermappingObjlist = new .....
......
ParametermappingObj element = null;
element = list.Find( x=>x.Name == "CC" && x.Id="123-CHISI");
if (element!= null)
list.Remove(element);
Oct 3 '08 #5
On Oct 3, 10:46 am, Göran Andersson <gu...@guffa.comwrote:
Tony Johansson wrote:
Hello!
I forgot to say that I use VS2008 and the collection consist of a
generic List<ParametermappingObj>
//Tony
"Tony Johansson" <t.johans...@logica.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...
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, 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(Parametermapping Obj 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.

--
Göran Andersson
_____http://www.guffa.com
You can use LINQ in a very elegant way. it will iterate until the
element is found for the first time.
Oct 3 '08 #6
On Fri, 03 Oct 2008 12:41:35 -0700, Ignacio Machin ( .NET/ C# MVP )
<ig************@gmail.comwrote:
You can use LINQ in a very elegant way. it will iterate until the
element is found for the first time.
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?

Do you have a code example that illustrates how LINQ can solve the problem
more elegantly?

Pete
Oct 3 '08 #7
On Thu, 02 Oct 2008 17:01:35 -0700, Jibesh <ji*******@gmail.comwrote:
Tony,

You can also try Predicate method of List<T>.Remove(...), This method
also
searching through the list elements. Read MSDN for more details.
There is no such method, and in fact in the code you posted, you did not
use any such method. The only Remove() method takes an object reference,
not a predicate.

For a single element one could use the "Find" method as the code you
posted does, but that's not as efficient as doing the loop explicitly as
Göran suggests.

There is a List<T>.RemoveAll() method that does the looping for you, but
that's exactly what Göran already suggested.

Pete
Oct 3 '08 #8
Thanks Pete.

sorry i miss typed it.

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

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 02 Oct 2008 17:01:35 -0700, Jibesh <ji*******@gmail.comwrote:
>Tony,

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

There is no such method, and in fact in the code you posted, you did not
use any such method. The only Remove() method takes an object reference,
not a predicate.

For a single element one could use the "Find" method as the code you
posted does, but that's not as efficient as doing the loop explicitly as
Göran suggests.

There is a List<T>.RemoveAll() method that does the looping for you, but
that's exactly what Göran already suggested.

Pete

Oct 3 '08 #9
On Oct 3, 3:46 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 03 Oct 2008 12:41:35 -0700, Ignacio Machin ( .NET/ C# MVP )

<ignacio.mac...@gmail.comwrote:
You can use LINQ in a very elegant way. it will iterate until the
element is found for the first time.

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
Oct 3 '08 #10
On Fri, 03 Oct 2008 13:29:54 -0700, Ignacio Machin ( .NET/ C# MVP )
<ig************@gmail.comwrote:
[...]
You can use LINQ in a very elegant way. it will iterate until the
element is found for the first time.

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?
More code than what? So far, you haven't posted any LINQ code at all.
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
Calling Find() followed by Remove() enumerates the list (up to the desired
element) twice. Writing the loop explicitly allows for doing the
enumeration just once.

I'm still confused by your comment about LINQ. I don't see how LINQ comes
into it.

Pete
Oct 3 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: m. pollack | last post by:
Hi all, I'm still trying to get to the bottom of the problem I am having with the CollectionBase class and the Object Collection Editor. Briefly put, I am exposing a strongly-typed collection...
2
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
4
by: Anders Borum | last post by:
Hello! I'm am currently working on making a central cache component threadsafe, and was looking at the synchronized implementation of the Hashtable. I was wondering why you'd really want to...
7
by: Franck Diastein | last post by:
Hi, I'm trying to remove items from a collection this way: foreach(Object myO in ObjectCol){ if(myO != "xxx"){ myO.Remove(); } } But I'm having an error telling me that Collection was...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
1
by: Frank | last post by:
Hello, I use a collection to store objects. If I want to remove all objects, is it good programming practice to set the collection to nothing? Or should I remove every object individually? Any...
11
by: Robert W. | last post by:
Here's some pseudo-code that describes what I'm trying to do: foreach(object in collection) { if (certain-condition) collection.Remove(object); } The problem with this is that foreach gets...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.