This should be extremely simple but I'm still new to the language and don't grasp the basics yet.
What I have is:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim appEnums As New Dictionary(Of Integer, String)
appEnums.Add(1, "www.eva.com")
appEnums.Add(2, "www.eva2.com")
appEnums.Add(4, "www.evanetwork.com")
appEnums.Add(5, "www.eva3.com")
appEnums.Add(6, "www.eva4.com")
appEnums.Add(7, "www.eva5.com")
appEnums.Add(8, "www.eva6.com")
appEnums.Add(9, "www.eva1.com")
appEnums.Add(10, "www.eva7.com")
Dim startSite As Integer = 1
Dim mainSite As Integer = 4
Dim returnSite As Integer = 5
End Sub
I need a query with returns a website (from the dictionary values) OR nothing if it cannot find one, based on those assumption:
- The dictionary key must be greater than starSite and it must not be = to mainSite or returnSite (both have to be excluded)
Sorry I didn't know this forum was moderated. Anyway I solved it myself but if you have a more elegant solution it would be appreciated.
Here is my solution:
Dim resultSite As String
Dim exludeSite As Integer() = New Integer() {mainSite, returnSite}
Try
resultSite = appEnums.Where(Function(x) x.Key > startSite _
AndAlso x.Key Not exludeSite.Contains(x.Key)) _
.OrderBy(Function(x) x.Key).Select(Function(x) x.Value).First()
Catch
resultSite = Nothing
End Try
manight
Member
59 Points
60 Posts
LINQ query on a Dictionary
May 23, 2012 06:04 PM|LINK
This should be extremely simple but I'm still new to the language and don't grasp the basics yet.
What I have is:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim appEnums As New Dictionary(Of Integer, String) appEnums.Add(1, "www.eva.com") appEnums.Add(2, "www.eva2.com") appEnums.Add(4, "www.evanetwork.com") appEnums.Add(5, "www.eva3.com") appEnums.Add(6, "www.eva4.com") appEnums.Add(7, "www.eva5.com") appEnums.Add(8, "www.eva6.com") appEnums.Add(9, "www.eva1.com") appEnums.Add(10, "www.eva7.com") Dim startSite As Integer = 1 Dim mainSite As Integer = 4 Dim returnSite As Integer = 5 End SubI need a query with returns a website (from the dictionary values) OR nothing if it cannot find one, based on those assumption:
- The dictionary key must be greater than starSite and it must not be = to mainSite or returnSite (both have to be excluded)
Any hint?
manight
Member
59 Points
60 Posts
Re: LINQ query on a Dictionary
May 24, 2012 10:45 AM|LINK
Sorry I didn't know this forum was moderated. Anyway I solved it myself but if you have a more elegant solution it would be appreciated.
Here is my solution:
Dim resultSite As String Dim exludeSite As Integer() = New Integer() {mainSite, returnSite} Try resultSite = appEnums.Where(Function(x) x.Key > startSite _ AndAlso x.Key Not exludeSite.Contains(x.Key)) _ .OrderBy(Function(x) x.Key).Select(Function(x) x.Value).First() Catch resultSite = Nothing End Try