none
Running total in LINQ (VB Version) RRS feed

  • Question

  • I want to calculate a running total using LINQ (VB.NET). I have come across a C# example (below), but can't when I translate it into a VB version, my running total is always 0. I can't seem to get a VB version. Can anyone help?

    static void Main(string[] args)
            {
                List<int> list = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007};
                int running_total = 0;

                var result_set =
                    from x in list
                    select new
                    {
                        num = x,
                        running_total = (running_total = running_total + x)
                    };

                foreach (var v in result_set)
                {
                    Console.WriteLine( "list element: {0}, total so far: {1}",
                        v.num,
                        v.running_total);
                }
               
                Console.ReadLine();
            }

    Thursday, July 29, 2010 1:13 PM

Answers

  • Hi CraigMuckleston-EMaC,

     

    In VB.NET, you can do something like this:

     

    Dim list As New List(Of Integer)() From {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007}
    
    Dim running_total As Integer = 0
    
    Dim result_set = From x In list
             Select New With {.num = x, .running_total = Sum(running_total, x)}
    
    For Each v In result_set
      Console.WriteLine("list element: {0}, total so far: {1}", v.num, v.running_total)
    Next v
    
    
    Private Function Sum(ByRef rt As Integer, ByVal x As Integer) As Integer
      rt = rt + x
      Return rt
    End Function
    
    

    Best regards,
    Alex Liang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 30, 2010 8:09 AM
    Moderator

All replies

  • static void Main(string[] args)
            {
                List<int> list = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007};

               int running_total = 0;
                var result_set =
                    from x in list
                    select new
                    {
                         num = x,
                        running_total +=x
                    };

                foreach (var v in result_set)
                {
                    Console.WriteLine( "list element: {0}, total so far: {1}",
                        v.num,
                        v.running_total);
                }
               
                Console.ReadLine();
            }

    Please post your vb version, the one that doesn't work

    Regards

    Thursday, July 29, 2010 2:24 PM
  •  

     

    Dim list As New List(Of Integer)

    list.Add(1)

    list.Add(53)

    list.Add(343)

    list.Add(8)

    list.Add(12)

     

     

    Dim running_total As Integer = 0

     

     

    Dim result_set = From x In list

     

     

    Select New With {.running_total = running_total + x}

    Thursday, July 29, 2010 2:32 PM
  • I'm not a VB programmer but that seems fine, perhaps you can step in to see the value change or use a watch to do so.

    Is this an exercise?  Because is simpler to use Count (it does this for you automatically)

    Regards

    Thursday, July 29, 2010 4:16 PM
  • Hi CraigMuckleston-EMaC,

     

    In VB.NET, you can do something like this:

     

    Dim list As New List(Of Integer)() From {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007}
    
    Dim running_total As Integer = 0
    
    Dim result_set = From x In list
             Select New With {.num = x, .running_total = Sum(running_total, x)}
    
    For Each v In result_set
      Console.WriteLine("list element: {0}, total so far: {1}", v.num, v.running_total)
    Next v
    
    
    Private Function Sum(ByRef rt As Integer, ByVal x As Integer) As Integer
      rt = rt + x
      Return rt
    End Function
    
    

    Best regards,
    Alex Liang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 30, 2010 8:09 AM
    Moderator