VB.NET and LINQ query on a dictionary - Stack Overflow


Wed Jun 29 2022 18:02:56 GMT+0000 (Coordinated Universal Time)

Saved by @dhfinch #vb.net #linq #dictionary

Dim resultSite As String =
    appEnums.OrderBy(Function(kvp) kvp.Key)
        .SkipWhile(Function(kvp) kvp.Key <= startSite)
        .Where(Function(kvp) kvp.Key <> mainSite AndAlso kvp.Key <> returnSite)
        .Select(Function(kvp) kvp.Value).FirstOrDefault()

I need a simple LINQ query on VB.NET on the dictionary below, but I'm really new to the subject so I need your help. 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 What I need here is to retrieve a SINGLE website (dictionary value of string type) or NOTHING (if the query cannot find an affordable result) given those rules: The dictionary key (integer) must be greater than startSite and not equal to mainSite or returnSite (both must be exluded from the result)