So I have this code to set up a list of String arrays from a query I have set up called userContractQuery
:
Dim QueryResults As New List(Of String())
For Each record In userContractQuery
QueryResults.Add({record.consultantName, record.contractBudget, record.contractID})
Next
I then am trying to get the string variable of the consultant name and add it to a label of a table layout panel called tblShowAssignment
:
For dataColumn As Integer = 0 To QueryResults.Count - 1
Dim lblData As New Label
tblShowAssignment.Controls.Add(lblData)
lblData.Font = New Font("Arial", 10)
lblData.Anchor = AnchorStyles.Bottom
lblData.Anchor = AnchorStyles.Top
lblData.Anchor = AnchorStyles.Left
lblData.Anchor = AnchorStyles.Right
lblData.TextAlign = ContentAlignment.MiddleCenter
If dataColumn = 0 Then lblData.Text = QueryResults(dataColumn, 0)
The QueryResults(dataColumn, 0)
continues to be underlined no matter what I do and I can't figure out how to get the specific string variable I need. Any help would be appreciated!
CodePudding user response:
QueryResults
is a list of string arrays, not a multidimensional array. So, each element of the list is an array. In order to get an element of one of the inner arrays, you need to access its index from the array itself, not the outer list.
You should use:
lblData.Text = QueryResults(dataColumn)(0)
This is equivalent to:
Dim currentArray As String() = QueryResults(dataColumn)
lblData.Text = currentArray(0)
That being said, it doesn't really seem necessary (or elegant) to create a list of arrays in this case. Why don't you just access the records directly? For example:
Dim records = userContractQuery.ToList()
For dataColumn As Integer = 0 To records.Count - 1
'...
'...
dataColumn = 0 Then lblData.Text = records(dataColumn).consultantName
Next
CodePudding user response:
Differentiating properties with an array index causes an unmaintainable loose coupling so you could make a class to hold the query results so you can access the property by name instead of a number,
Public Class Foo
Public Property consultantName As String
Public Property contractBudget As String
Public Property contractID As String
End Class
Dim QueryResults = userContractQuery.Select(Function(record) New Foo() With {
.consultantName = record.consultantName,
.contractBudget = record.contractBudget,
.contractID = record.contractID}).ToList()
or instead of a new class, just use a Tuple
Dim QueryResults = userContractQuery.Select(Function(record) (
record.consultantName,
record.contractBudget,
record.contractID)).ToList()
which is then used the same way as the Class solution when retrieving, like this
lblData.Text = QueryResults(dataColumn).consultantName
' vs
lblData.Text = QueryResults(dataColumn)(0)
and while you're at it, you assign the label text when dataColumn = 0, so you could hard-code that, but since you want the first record why not use IEnumerable.First,
lblData.Text = QueryResults(0).consultantName
' or, better yet
lblData.Text = QueryResults.First().consultantName
so you end up with readable code in plain English (which is kind of a benefit of using LINQ) and I'm also wondering why you don't just read directly from the userContractQuery
because, though I don't know what it is, it looks like it is IEnumerable, such as this
userContractQuery.First().consultantName
which I understand if you don't want to / can't if the query has been disposed / out of scope (probably why you made the other list in the first place?) but yea, there should be something good for you in here.
CodePudding user response:
you can use this code
QueryResults.ToList()(dataColumn)(0)
Or
QueryResults(dataColumn)(0)