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

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

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.
Aug 1 '08 #1
5 3987
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
Aug 1 '08 #2
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
Aug 1 '08 #3
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, intunique = 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
Aug 1 '08 #4
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, intunique = 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
Aug 1 '08 #5
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, intunique = 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
Aug 1 '08 #6

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

Similar topics

4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
4
by: colson | last post by:
Hi, If I have a class A, and a List<List<object>> containing instances of A. How do I explicitly cast List<List<object>> as List<List<A>>?
13
by: Murat Ozgur | last post by:
Hello, Is there any difference between ArrayList and List<object? Which one should I use ? Thanks.
2
by: Jeff | last post by:
..NET 2.0 I'm wondering if it is possible set the DataGridView.DataSource to a List<objectcollection. Where object is a class derived from the object class. If it's possible then my question...
9
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.