List(Of ObjectWithProperties) distinct property values
-
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.
-
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.
-
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.
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:
-
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.
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[^]
-
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[^]
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.
-
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[^]
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