473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting items in a list

Hi,

My app populates a list of custom objects from data fetched from a database.

What i want to do now is filter the list and remove items from the list that
don't match the filter criteria.

I was thinking of using foreach to traverse the list and dynamically remove
items that fail the filter however I am having difficulty removing an item
from the list as i don't know its postion in the list.

Can anyone tell me how to do this as i want to avoid creating another list
to hold filtered items.

Thanks
Macca
Oct 13 '06 #1
1 2121
1.1 or 2.0?

In 2.0 you can use a predicate and List<Tto do this for you; just replace
"int" with your class, and the test with your "delete me" condition...

static void Main(string[] args)
{
// add some dummy data
List<intnumbers = new List<int>();
for (int i = 1; i < 20; i++)
numbers.Add(i);

// remove everything that is divisible by 3
numbers.RemoveAll(delegate(int testItem)
{
// remove true if we meet the condition, i.e.
// those that we want to remove
return testItem % 3 == 0; // divisible by 3 =true
});

// look at what is left;
foreach (int item in numbers)
{
Console.WriteLine(item);
}
}
Oct 13 '06 #2

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

Similar topics

6
1464
by: Amit Kela | last post by:
I am using ASP with SQL for my database. The problem I have is that even after I have ordered certain items from the shopping cart table on the webpage, I cannot remove them - that is the entire...
2
1757
by: KraftDiner | last post by:
I have a list, and within it, objects are marked for deletion. However when I iterate through the list to remove objects not all the marked objects are deleted.. here is a code portion: i = 0...
1
1685
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
10
4375
by: A_PK | last post by:
I am writing mobile application using Vb.net when i click the last row of list view, and try to delete it.... will promtpy the following message "Additional information:...
0
1511
by: steven.shannon | last post by:
Hello, I'm writing an app that involves deleting all the items in a specified Outlook folder regardless of item type (i.e. Contacts, Tasks, etc.). This code was ported from VBA where it worked...
2
2821
by: Ted | last post by:
is there an example of how one would go about selectively deleting items in IE's history based on URL names using VB.NET? i have checked every resource i know and come up short.
25
3840
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
9
2410
by: Rhamphoryncus | last post by:
The problems of this are well known, and a suggestion for making this easier was recently posted on python-dev. However, I believe this can be done just as well without a change to the language. ...
3
2235
by: laurasaur | last post by:
Hi everyone, I have 2 listboxes that I need to move items between, they are both bound to DataTables which get populated from the database with a list of clients. Im getting a few problems,...
0
7278
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
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
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...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.