Retrieving Unique Items from a List

  • Thread starter Thread starter amir
  • Start date Start date
A

amir

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?
 
amir said:
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,Item> grp 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.
--
 
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 said:
[...]
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<ListItem> listDict;

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

listDict.Add(item);
}

foreach (List<ListItem> listItems 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
 

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

Back
Top