locked
Lambda Aggregate works in C# but not in VB.net RRS feed

  • Question

  • User1558924997 posted

    C# Code: works fine

      string[] arr = new string[] { "11", "12", "13", "14", "15" };
    
       string cons = "79";
    
       var str = arr.Aggregate(string.Empty, (y, z) => y += string.Format("({0},{1}),", cons, z)).Trim(',');
    


    but VB.net Code is showing syntax error in converting y += into y &=

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    
    Dim cons As String = "79"
    
    Dim str = arr.Aggregate(String.Empty, Function(y, z) y &= String.Format("({0},{1}),", cons, z)).Trim(","C)
    

    In Vb.net error is at y &=, any idea to write this correctly.

    when we do (y = y & string) then comparision is done, and as expected  False is returned.  

    Required Output:

    (79,11),(79,12),(79,13),(79,14),(79,15)

    Friday, May 10, 2013 9:10 AM

Answers

  • User-158764254 posted

    Try it like this:

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    Dim cons As String = "79"
    Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Format("{2}({0},{1}),", cons, z, y)).Trim(","c)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 11, 2013 8:41 PM
  • User-861818263 posted

    I have tested below solution working properly

    Solution#1 

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    
            Dim cons As String = "79"
    
            Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Concat(y, String.Format("({0},{1}),", cons, z))).Trim(","c)

    Solution#2

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
            Dim cons As String = "79"
            Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Format("{0}({1},{2}),", y, cons, z)).Trim(","c)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 9:53 AM

All replies

  • User-861818263 posted

    try below code :-

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    
    Dim cons As String = "79"
    
    Dim str = arr.Aggregate(String.Empty, Function(y, z) y += String.Format("({0},{1}),", cons, z)).Trim(","C)



    Friday, May 10, 2013 9:20 AM
  • User1558924997 posted

    Sameer,

    Have you tried it, this is same(for String) &= or += in VB.net ?

    It shows the same error.

    Warning: Comma, ) or a Valid Expression Continuation is required

    Friday, May 10, 2013 9:26 AM
  • User-158764254 posted

    Try it like this:

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    Dim cons As String = "79"
    Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Format("{2}({0},{1}),", cons, z, y)).Trim(","c)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 11, 2013 8:41 PM
  • User-861818263 posted

    I have tested below solution working properly

    Solution#1 

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
    
            Dim cons As String = "79"
    
            Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Concat(y, String.Format("({0},{1}),", cons, z))).Trim(","c)

    Solution#2

    Dim arr As String() = New String() {"11", "12", "13", "14", "15"}
            Dim cons As String = "79"
            Dim str = arr.Aggregate(String.Empty, Function(y, z) String.Format("{0}({1},{2}),", y, cons, z)).Trim(","c)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 9:53 AM