473,382 Members | 1,423 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,382 software developers and data experts.

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(item);
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 2162
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(item);
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.<theCriteria>;

foreach(IGrouping<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**@discussions.microsoft.com
wrote:
[...]
foreach (item in the list)
mylist = list.findall(item);
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<KeyType, List<ListItem>dict = new Dictionary<KeyType,
List<ListItem>>();

foreach (ListItem item in list)
{
List<ListItemlistDict;

if (!dict.TryGetValue(item.KeyProperty, out listDict))
{
listDict = new List<ListItem>();
dict.Add(item.KeyProperty, listDict);
}

listDict.Add(item);
}

foreach (List<ListItemlistItems 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**@discussions.microsoft.com>
wrote:
[...]
foreach (item in the list)
mylist = list.findall(item);
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<KeyType, List<ListItem>dict = new Dictionary<KeyType,
List<ListItem>>();

foreach (ListItem item in list)
{
List<ListItemlistDict;

if (!dict.TryGetValue(item.KeyProperty, out listDict))
{
listDict = new List<ListItem>();
dict.Add(item.KeyProperty, listDict);
}

listDict.Add(item);
}

foreach (List<ListItemlistItems 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
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...
22
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
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...
6
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...
0
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...
11
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...
3
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...
2
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.