.net - Linq statement VB.NET List -
i have list of increasing integer n1, n2, n3, ...ni... n100. want write linq statement and/or lambda expression following: given integer n, search through list , 2 contiguous items in list ni , n(i+1) such n(i) <= n <= n(i+1). how can using linq statement in vb.net ? know how using normal loop. want use linq statement. thank in advance.
i want learn equivalence between normal loop , linq statement in particular case.
edit: i've done using normal loop follows:
sub get2items(byval list list(of integer), byval n integer, byref item1 integer, byref item2 integer) integer = 0 list.count - 2 if list(i) <= n andalso n <= list(i + 1) item1 = list(i) item2 = list(i + 1) exit end if next end sub
i wrote in c# , converted vb using dotnetfiddle, might wrong syntactically in 1 or 2 places, gist.
in essence, zip input list offsetted version of (which means want make sure you're doing on safely-reenumerated ienumerables, or else calling toarray() @ beginning set variable use. isn't issue since parameter type list(of integer), wouldn't bad idea change ienumerable(of integer) have stronger contract , more usability.) zip them anonymous type temporary storage, before filtering based on condition set in original for loop.
dim zipped = list.zip(list.skip(1), function(a, b) new {.a = a, .b = b}) dim ret = zipped.where(function(c) c.a <= n andalso n <= c.b) each v in ret item1 = v.a item2 = v.b next it'd better return ienumerable(of tuple(of integer, integer)), in case call different .select on ret return correct type, rather dealing byref parameters you're doing.
Comments
Post a Comment