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

Discussion in 'ASP .Net' started by Paul, Aug 1, 2008.

  1. Paul

    Paul Guest

    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.
    Paul, Aug 1, 2008
    #1
    1. Advertising

  2. Paul

    Lloyd Sheen Guest

    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
    Lloyd Sheen, Aug 1, 2008
    #2
    1. Advertising

  3. Paul

    Paul Guest

    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
    >
    Paul, Aug 1, 2008
    #3
  4. 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
    Göran Andersson, Aug 1, 2008
    #4
  5. Paul

    Paul Guest

    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
    >
    Paul, Aug 1, 2008
    #5
  6. Paul

    Paul Guest

    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
    > >
    Paul, Aug 1, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    700
    Patrice
    May 12, 2005
  2. Klixx0r
    Replies:
    1
    Views:
    2,091
    Gerbrand van Dieijen
    Dec 8, 2004
  3. Ben Davies
    Replies:
    3
    Views:
    352
    Erik Max Francis
    Jan 14, 2004
  4. ToshiBoy
    Replies:
    6
    Views:
    844
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    354
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page