Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I have a list of integer
Dim IntList as list(of integer) = {5,7,8,10,15,24,26}

I want subtraction each item from next item,
(7-5),(8-7),(10-8),(15-10),(24-15),(26-24)
ResultList = {2,1,2,5,9,2}


What I have tried:

I have no idea.
Dim ResultList = IntList.Where(Function(x, i) New KeyValuePair(Of Byte, Integer)(x.Key, i)).ToList
Posted
Updated 20-Aug-18 7:41am
v2

Try this:

VB
Sub Main()

    Dim IntList As List(Of Integer) = New List(Of Integer)

    IntList.Add(5)
    IntList.Add(7)
    IntList.Add(8)
    IntList.Add(10)
    IntList.Add(15)
    IntList.Add(24)
    IntList.Add(26)
    '{5, 7, 8, 10, 15, 24, 26}


    Dim ResList As List(Of Integer) = New List(Of Integer)

    For i = 0 To IntList.Count - 2
        ResList.Add(IntList(i + 1) - IntList(i))
    Next
End Sub
 
Share this answer
 
v2
Comments
gacar 20-Aug-18 11:44am    
Thanks. But i want linq solution.
Leo Chapiro 20-Aug-18 11:56am    
You wrote that you have had no idea, now you have an algorithm. It will not be difficult to change it to LINQ, there is even online tools for this purpose.
I found solution here

Dim IntList As List(Of Integer) = {5, 7, 8, 10, 15, 24, 26}.ToList

Dim ResultList = (From x In IntList Let nextindex = IntList.IndexOf(x) + 1 Let nextelement = IntList.ElementAt(If(nextindex = IntList.Count, nextindex - 1, nextindex)) Select nextelement - x).ToList()

result.RemoveAt(IntList.Count - 1)

ResultList = {2,1,2,5,9,2}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900