How to convert any integer array to 1 And any '-1' to 0 With LINQ

Mansour_Dalir 1,856 Reputation points
2023-09-28T16:10:28.72+00:00

hi

-1 to 0 And Any To 1 Thank

dim sttDataToArray as String="-1,-1,-78,56,23,-1,-66789,23,-1,-1"

dim MyArrayBinary as String()=Split(sttDataToArray ,",")

'MyArrayBinary =Need LINQ Command

'Need Result MyArrayBinary To :0,0,1,1,1,0,1,1,0,0

'And Need Count Binary By LINQ Convert '0,0,1,1,1,0,1,1,0,0' to "0=2,1=3,0=1,1=2,0=2" And '0,0,1,1,1,0,1,1,0,0' to "2,3,1,2,2"

dim CountBinary as string="0=2,1=3,0=1,1=2,0=2"

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,713 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 30,931 Reputation points Microsoft Vendor
    2023-09-29T01:35:54.68+00:00

    Hi @Mansour_Dalir ,

    Please check if the following code helps.

            Dim sttDataToArray As String = "-1,-1,-78,56,23,-1,-66789,23,-1,-1"
            Dim MyArrayBinary As String() = Split(sttDataToArray, ",")
    
            Dim resultArray As Integer() = MyArrayBinary.Select(Function(x) If(Integer.TryParse(x, Nothing), If(Convert.ToInt32(x) = -1, 0, 1), 0)).ToArray()
    
            Dim finalResult As String() = resultArray.Select(Function(x) x.ToString()).ToArray()
    
            Dim countArray As New List(Of String)()
            Dim numberArray As New List(Of Integer)()
    
            Dim currentNumber As Integer = resultArray(0)
            Dim currentCount As Integer = 1
    
            For i As Integer = 1 To resultArray.Length - 1
                If resultArray(i) = currentNumber Then
                    currentCount += 1
                Else
                    countArray.Add($"{currentNumber}={currentCount}")
                    numberArray.Add(currentCount)
    
                    currentNumber = resultArray(i)
                    currentCount = 1
                End If
            Next
    
            countArray.Add($"{currentNumber}={currentCount}")
            numberArray.Add(currentCount)
    
            Dim resultString As String = String.Join(",", finalResult)
            Dim countResult As String = String.Join(",", countArray)
            Dim numberResult As String = String.Join(",", numberArray)
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 6,395 Reputation points
    2023-09-29T00:16:47.8366667+00:00

    Is it like this?

    Dim result = MyArrayBinary.Select(
        Function(x) If(Integer.Parse(x) = -1, 0, 1)).ToArray()
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.