471,337 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 2030
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by kevin parks | last post: by
22 posts views Thread by Claudio Jolowicz | last post: by
6 posts views Thread by Dave Hopper | last post: by
11 posts views Thread by garyhoran | last post: by
reply views Thread by rosydwin | last post: by

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.