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

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

1. 30,931 Reputation points Microsoft Vendor
2023-09-29T01:35:54.68+00:00

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

currentNumber = resultArray(i)
currentCount = 1
End If
Next

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

``````Dim result = MyArrayBinary.Select(