Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to get just unique values from a List<object>

Reply
Thread Tools

How to get just unique values from a List<object>

 
 
Paul
Guest
Posts: n/a
 
      08-01-2008
Hi I have a list of type object. The object has an ID as one of the elements
and I would like to create another list that just has objects with unique IDs.
For example
in the list if I have
listofobject[0].ID = 1
listofobject[1].ID =1
listofobject[2].ID = 2
I would like new list with only
listofobject[0].ID =1
listofobject[1].ID = 2
Thanks.
--
Paul G
Software engineer.
 
Reply With Quote
 
 
 
 
Lloyd Sheen
Guest
Posts: n/a
 
      08-01-2008
Paul wrote:
> Hi I have a list of type object. The object has an ID as one of the elements
> and I would like to create another list that just has objects with unique IDs.
> For example
> in the list if I have
> listofobject[0].ID = 1
> listofobject[1].ID =1
> listofobject[2].ID = 2
> I would like new list with only
> listofobject[0].ID =1
> listofobject[1].ID = 2
> Thanks.

If you are using VS 2008 then you can do a linq query.

LS
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      08-01-2008
Hi thanks for the response. Still using vs2005, ended up just going through
the list and if in second list do nothing, else add to second list. The
second list is the one that ends up with just the unique values.
--
Paul G
Software engineer.


"Lloyd Sheen" wrote:

> Paul wrote:
> > Hi I have a list of type object. The object has an ID as one of the elements
> > and I would like to create another list that just has objects with unique IDs.
> > For example
> > in the list if I have
> > listofobject[0].ID = 1
> > listofobject[1].ID =1
> > listofobject[2].ID = 2
> > I would like new list with only
> > listofobject[0].ID =1
> > listofobject[1].ID = 2
> > Thanks.

> If you are using VS 2008 then you can do a linq query.
>
> LS
>

 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      08-01-2008
Paul wrote:
> Hi thanks for the response. Still using vs2005, ended up just going through
> the list and if in second list do nothing, else add to second list. The
> second list is the one that ends up with just the unique values.


That doesn't scale very well, as checking if the list contains a value
gets considerably slower as the list grows. You should use a dictionary
for checking if the values exist or not:

Dictionary<int, int> unique = new Dictionary<int, int>();
foreach (int value in listofobjects) {
if (unique.ContainsKey(value)) {
// counts occurances - you can skip this if you don't want it:
unique[value]++;
} else {
unique.Add(value, 1);
}
}


--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-01-2008
thanks for the response. I do have some code working but will give it a try.
I converted the application from a windows to a console app as I need to
schedule it to run through sql server agent. Anyhow I am getting the error
on two methods now with the console app, worked fine in the windows app. The
error is (an object reference is required for the non static field, method or
property). This occures on the line were I call a method that I am passing 4
string lists into, so looks like
method2(emails, reports, names, days).
any ideas.
--
Paul G
Software engineer.


"Göran Andersson" wrote:

> Paul wrote:
> > Hi thanks for the response. Still using vs2005, ended up just going through
> > the list and if in second list do nothing, else add to second list. The
> > second list is the one that ends up with just the unique values.

>
> That doesn't scale very well, as checking if the list contains a value
> gets considerably slower as the list grows. You should use a dictionary
> for checking if the values exist or not:
>
> Dictionary<int, int> unique = new Dictionary<int, int>();
> foreach (int value in listofobjects) {
> if (unique.ContainsKey(value)) {
> // counts occurances - you can skip this if you don't want it:
> unique[value]++;
> } else {
> unique.Add(value, 1);
> }
> }
>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
>

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-01-2008
Got it working!
--
Paul G
Software engineer.


"Paul" wrote:

> thanks for the response. I do have some code working but will give it a try.
> I converted the application from a windows to a console app as I need to
> schedule it to run through sql server agent. Anyhow I am getting the error
> on two methods now with the console app, worked fine in the windows app. The
> error is (an object reference is required for the non static field, method or
> property). This occures on the line were I call a method that I am passing 4
> string lists into, so looks like
> method2(emails, reports, names, days).
> any ideas.
> --
> Paul G
> Software engineer.
>
>
> "Göran Andersson" wrote:
>
> > Paul wrote:
> > > Hi thanks for the response. Still using vs2005, ended up just going through
> > > the list and if in second list do nothing, else add to second list. The
> > > second list is the one that ends up with just the unique values.

> >
> > That doesn't scale very well, as checking if the list contains a value
> > gets considerably slower as the list grows. You should use a dictionary
> > for checking if the values exist or not:
> >
> > Dictionary<int, int> unique = new Dictionary<int, int>();
> > foreach (int value in listofobjects) {
> > if (unique.ContainsKey(value)) {
> > // counts occurances - you can skip this if you don't want it:
> > unique[value]++;
> > } else {
> > unique.Add(value, 1);
> > }
> > }
> >
> >
> > --
> > Göran Andersson
> > _____
> > http://www.guffa.com
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
JDO Question : Is it possible to get unique values like DISTINCT in SQL? Klixx0r Java 1 12-08-2004 10:34 AM
Need help with getting unique values in a file. Koncept Perl 0 04-30-2004 05:31 PM



Advertisments