473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving Unique Items from a List

Hi,

I have a Generic Object that has a private List field item.

I populate the List in a different function

I use a FOREACH LOOP with a FindAll function to get all items that have a
certain match for data in the list and do something to it.

The problem is that it repeats for all data items in the list and not
exclude the ones already processed. How can I exclude the ones already
processed.

what I have is

foreach (item in the list)
mylist = list.findall(it em);
foreach(myitem in mylist)
dosomething

the problem occurs in the outer foreach where it already has processed some
items from the original list. how do i exclude them?

Jun 27 '08 #1
3 2191
amir wrote:
Hi,

I have a Generic Object that has a private List field item.

I populate the List in a different function

I use a FOREACH LOOP with a FindAll function to get all items that
have a certain match for data in the list and do something to it.

The problem is that it repeats for all data items in the list and not
exclude the ones already processed. How can I exclude the ones
already processed.

what I have is

foreach (item in the list)
mylist = list.findall(it em);
foreach(myitem in mylist)
dosomething

the problem occurs in the outer foreach where it already has
processed some items from the original list. how do i exclude them?
I am not sure that I 100% understand what you want to do here, but it
sounds kinda like you want some kind of group processing right?

so maybe something like (using linq)

var groupList = from item in itemList
group item on item.<theCriter ia>;

foreach(IGroupi ng<CriteriaType ,Itemgrp in groupList)
{
// create new tab or page or section break / or whatever with grp.key
foreach (Item item in grp)
{
// process each item in group.
}

}

Rgds Tim.
--

Jun 27 '08 #2
On Thu, 15 May 2008 15:40:02 -0700, amir <am**@discussio ns.microsoft.co m
wrote:
[...]
foreach (item in the list)
mylist = list.findall(it em);
foreach(myitem in mylist)
dosomething

the problem occurs in the outer foreach where it already has processed
some
items from the original list. how do i exclude them?
If you really must process your list items in groups according to your
FindAll() results, I think using LINQ as Tim suggests would work well.
However, I have to wonder why you want to do this. Nothing in the code
you posted indicates an actual need to do this, and it seems like you'd be
better off just enumerating the list and processing each element one by
one. The way you've shown it, you've basically got an O(N^2) algorithm
even if we assume you somehow address the duplicated item issue at no cost
(which isn't a realistic assumption).

If you can't use LINQ and you must process in groups, an alternative
solution would be to use a Dictionary where the key for the dictionary is
the same as whatever criteria you're using for the FindAll() search.
Then, when enumerating each item in the list, rather than doing work
during that enumeration, simply build up lists of elements in your
Dictionary based on that key, and then enumerate those lists later:

Dictionary<KeyT ype, List<ListItem>d ict = new Dictionary<KeyT ype,
List<ListItem>> ();

foreach (ListItem item in list)
{
List<ListItemli stDict;

if (!dict.TryGetVa lue(item.KeyPro perty, out listDict))
{
listDict = new List<ListItem>( );
dict.Add(item.K eyProperty, listDict);
}

listDict.Add(it em);
}

foreach (List<ListIteml istItems in dict.Values)
{
foreach (ListItem item in listItems)
{
// do something
}
}

That's only O(N) instead of O(N^2) and IMHO makes it a bit more clear that
you specifically are trying to group the items before processing.

Pete
Jun 27 '08 #3
Hello Peter and Tim,

Linq was my first approach but I am bogged down with the users not having
the latest framework and and compatible machine. However, the TryGetValue is
the right solution.

Thanks much fellas.

"Peter Duniho" wrote:
On Thu, 15 May 2008 15:40:02 -0700, amir <am**@discussio ns.microsoft.co m>
wrote:
[...]
foreach (item in the list)
mylist = list.findall(it em);
foreach(myitem in mylist)
dosomething

the problem occurs in the outer foreach where it already has processed
some
items from the original list. how do i exclude them?

If you really must process your list items in groups according to your
FindAll() results, I think using LINQ as Tim suggests would work well.
However, I have to wonder why you want to do this. Nothing in the code
you posted indicates an actual need to do this, and it seems like you'd be
better off just enumerating the list and processing each element one by
one. The way you've shown it, you've basically got an O(N^2) algorithm
even if we assume you somehow address the duplicated item issue at no cost
(which isn't a realistic assumption).

If you can't use LINQ and you must process in groups, an alternative
solution would be to use a Dictionary where the key for the dictionary is
the same as whatever criteria you're using for the FindAll() search.
Then, when enumerating each item in the list, rather than doing work
during that enumeration, simply build up lists of elements in your
Dictionary based on that key, and then enumerate those lists later:

Dictionary<KeyT ype, List<ListItem>d ict = new Dictionary<KeyT ype,
List<ListItem>> ();

foreach (ListItem item in list)
{
List<ListItemli stDict;

if (!dict.TryGetVa lue(item.KeyPro perty, out listDict))
{
listDict = new List<ListItem>( );
dict.Add(item.K eyProperty, listDict);
}

listDict.Add(it em);
}

foreach (List<ListIteml istItems in dict.Values)
{
foreach (ListItem item in listItems)
{
// do something
}
}

That's only O(N) instead of O(N^2) and IMHO makes it a bit more clear that
you specifically are trying to group the items before processing.

Pete
Jun 27 '08 #4

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

Similar topics

2
2303
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a tough problem for me (though i am getting closer). i have been mucking with a lot of data in a dictionary that looks like:
22
2760
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
0
1582
by: Alistair | last post by:
Hi all, I am creating a database based site that keeps track of books, who has read them and the comments they have. After a little help in M.P.I.asp.DB I managed to create a database (access 2000) as follows USERS TABLE
6
9653
by: Dave Hopper | last post by:
Hi I am using the following SQL to retrieve a value in a list box using a unique ID held in the list box call cntID. The list box is used on an order form to list appointments that have been made for service staff. My problem is the SQL is not retrieving the value of the listbox. I have tested the SQL by entering a fixed value of a valid entry in the listbox and it works fine, but as soon as I replace the fixed value with...
0
1885
by: yeltsin27 | last post by:
I need some advice on handling dynamically added controls in a GridView. My app takes an uploaded CSV file containing addresses, converts it to a DataTable, databinds the DataTable to a GridView, accepts information via dropdowns embedded in the first row of the GridView about which incoming fields should go to which database fields, and writes the data to the database. The incoming data has an arbitrary number of columns so I create...
11
7806
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the application ie sometimes I want to cycle through the values in the collection in DateTime order while at other times in TimeSpan order. Ideally I would like multiple keys - such as Timespan within DateTime order but maybe that is asking too much.
3
8325
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now . Now I want to add 5 to the unique item stack so it should now be: . The item added is pulled from the stack, then pushed. The stack should also have a maximum size of 4 items. I add 4 and 7
2
2711
by: Vahagn | last post by:
Hi, how do I retrieve the chosen value from a RadioButtonList? I have a RadioButtonList that is populated dynamically with a "Next" button. Ie, I have a list of questions each with 2-3 answer options. When the "Next" button is pressed, it fetches the next question from the DB and the corresponding set of answers (only one of which is true). Then when the user chooses his answer / radio button from the list, he uses the same "Next" button to...
1
1475
by: Anjan Bhowmik | last post by:
Suppose i have a multi-select list box which loads data from a table in a SQL Server 2005 Database. This list box loads all rows from the table, which has around 2000 rows. So when databinding all 2000 rows will be fetched from database and bound to the List box. Which is fairly a time consuming process. Now when i insert 5 more values into the database and call list box's DataBind() method, it will return all 2005 rows. So, continuing...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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 we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.