Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. List(Of ObjectWithProperties) distinct property values

List(Of ObjectWithProperties) distinct property values

Scheduled Pinned Locked Moved Visual Basic
questionhelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johan Hakkesteegt
    wrote on last edited by
    #1

    Hi, Let's say that I have an object called Employee with the properties Name and Department. Next I have a List(Of Employee) with say 5 employees in it. Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown. How can I get the distinct departments from my list ? There is List.Distinct, but it seems I can only get distinct entire objects (employees) through that. Any help much appreciated. Best regards, Johan

    My advice is free, and you may get what you paid for.

    L D 3 Replies Last reply
    0
    • J Johan Hakkesteegt

      Hi, Let's say that I have an object called Employee with the properties Name and Department. Next I have a List(Of Employee) with say 5 employees in it. Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown. How can I get the distinct departments from my list ? There is List.Distinct, but it seems I can only get distinct entire objects (employees) through that. Any help much appreciated. Best regards, Johan

      My advice is free, and you may get what you paid for.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Probably by creating another List (or other collection class) which only contains departments. Go through all your employee records adding the department to the second list.

      1 Reply Last reply
      0
      • J Johan Hakkesteegt

        Hi, Let's say that I have an object called Employee with the properties Name and Department. Next I have a List(Of Employee) with say 5 employees in it. Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown. How can I get the distinct departments from my list ? There is List.Distinct, but it seems I can only get distinct entire objects (employees) through that. Any help much appreciated. Best regards, Johan

        My advice is free, and you may get what you paid for.

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        It appears that there is an overload to the .Distinct method where you pass it a function which does the comparison for you. In this function, you would implement the compare of the department field only. Check this out ... http://msdn.microsoft.com/en-us/library/vstudio/bb920306(v=vs.90).aspx[^] And this example code ... http://msdn.microsoft.com/en-us/library/vstudio/bb338049(v=vs.90).aspx[^] Hope this helps. :thumbsup:

        1 Reply Last reply
        0
        • J Johan Hakkesteegt

          Hi, Let's say that I have an object called Employee with the properties Name and Department. Next I have a List(Of Employee) with say 5 employees in it. Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown. How can I get the distinct departments from my list ? There is List.Distinct, but it seems I can only get distinct entire objects (employees) through that. Any help much appreciated. Best regards, Johan

          My advice is free, and you may get what you paid for.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Johan Hakkesteegt wrote:

          Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown.

          One employee in one department, not one employee in multiple departments. Check.

          Imports System.Collections.Generic
          Module Module1

          ' Let's say that I have an object called Employee with the properties Name and Department.
          Class Employee
              Public Property Name As String
              Public Property Department As String
          End Class
          
          Sub Main()
          
              Dim zeList As New List(Of Employee)
              zeList.Add(New Employee() With {.Name = "John", .Department = "IT"})
              zeList.Add(New Employee() With {.Name = "Jane", .Department = "IT"})
              zeList.Add(New Employee() With {.Name = "Pete", .Department = "Management"})
          
              Dim departments As IEnumerable(Of String) = zeList.Select(Function(e) e.Department).Distinct()
          
              For Each department As String In departments
                  Console.WriteLine(department)
              Next
              Console.ReadLine()
          
          End Sub
          

          End Module

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          J M 2 Replies Last reply
          0
          • L Lost User

            Johan Hakkesteegt wrote:

            Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown.

            One employee in one department, not one employee in multiple departments. Check.

            Imports System.Collections.Generic
            Module Module1

            ' Let's say that I have an object called Employee with the properties Name and Department.
            Class Employee
                Public Property Name As String
                Public Property Department As String
            End Class
            
            Sub Main()
            
                Dim zeList As New List(Of Employee)
                zeList.Add(New Employee() With {.Name = "John", .Department = "IT"})
                zeList.Add(New Employee() With {.Name = "Jane", .Department = "IT"})
                zeList.Add(New Employee() With {.Name = "Pete", .Department = "Management"})
            
                Dim departments As IEnumerable(Of String) = zeList.Select(Function(e) e.Department).Distinct()
            
                For Each department As String In departments
                    Console.WriteLine(department)
                Next
                Console.ReadLine()
            
            End Sub
            

            End Module

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            J Offline
            J Offline
            Johan Hakkesteegt
            wrote on last edited by
            #5

            Last night I woke up and figured Richard's answer myself, but this is the "one-liner" that I was after. Thanks Eddy !

            My advice is free, and you may get what you paid for.

            1 Reply Last reply
            0
            • L Lost User

              Johan Hakkesteegt wrote:

              Each of these 5 employees are member of one of 10 possible different departments. Perhaps they are each member of a different department, perhaps they are all members of the same department, etc. This is an unknown.

              One employee in one department, not one employee in multiple departments. Check.

              Imports System.Collections.Generic
              Module Module1

              ' Let's say that I have an object called Employee with the properties Name and Department.
              Class Employee
                  Public Property Name As String
                  Public Property Department As String
              End Class
              
              Sub Main()
              
                  Dim zeList As New List(Of Employee)
                  zeList.Add(New Employee() With {.Name = "John", .Department = "IT"})
                  zeList.Add(New Employee() With {.Name = "Jane", .Department = "IT"})
                  zeList.Add(New Employee() With {.Name = "Pete", .Department = "Management"})
              
                  Dim departments As IEnumerable(Of String) = zeList.Select(Function(e) e.Department).Distinct()
              
                  For Each department As String In departments
                      Console.WriteLine(department)
                  Next
                  Console.ReadLine()
              
              End Sub
              

              End Module

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Eddy Vluggen wrote:

              zeList.Select(Function(e) e.Department).Distinct()

              I looked and looked and wondered where the Function(e) comes from and what is it doing :doh: x=> So I have not looked at VB since before linq was invented.

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups