Consider the following class:
Public Class Employee
Public State As String
Public Dept As String
Public Status as Integer
End Class
I need to query a distinct value from a listEmployees
which is four records:
oEmp = New Employee
oEmp.Dept = "HR"
oEmp.State = "Kansas"
oEmp.Status = 1
listEmployees.Add(oEmp)
oEmp = New Employee
oEmp.Dept = "HR"
oEmp.State = "Texas"
oEmp.Status= 3
listEmployees.Add(oEmp)
oEmp = New Employee
oEmp.Dept = "HR"
oEmp.State = "Texas"
oEmp.Status= 5
listEmployees.Add(oEmp)
oEmp = New Employee
oEmp.Dept = "DEV"
oEmp.State = "Texas"
oEmp.Status= 7
listEmployees.Add(oEmp)
What I need the query to do is return distinct Deparments where State
= 'Texas'. But I need be able to add multiple cases to the Where
clause. So another query might need to return distinct Status's where State
= 'Texas', and Dept
= 'HR'.
There will always be only one field which I need distinct.
So the results to my first query example would be "HR" and "DEV" because those are the distinct Departments that have "Texas" as the State. The results to the second query would be "3" and "5" because those are the distinct Status's where the State is "Texas" and the Department is "HR".
UPDATE:
Here's what I've got working for Query #1:
Dim test = (From emp As Employee In listEmployees
Where emp.State = "Texas"
Select emp.Dept).Distinct()
However, if I specify something for State
that doesn't exist, such as "TTexas", I get the following exception (the code doesn't blow up but if I inspect the test
variable in Visual Studio after this line executes, I get this):
Exception of type 'System.Linq.SystemCore_EnumerableDebugViewEmptyException' was thrown. at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
Is this normal LINQ behavior?