0

I am trying to use a LINQ query to grab some data from a database, and since I am grabbing just one column of data I want to store it within a string list. This is the code I have.

Dim POList As New List(Of String)
Using dbContext As New DBLINQDataContext
    Dim query = (From o In dbContext.Orders
                 Where o.Order_Number.StartsWith(JobNumber)
                 Select o.Order_Number)
    POList = query.ToList()
End Using
MessageBox.Show(POList.ToString())

When I run this the data in the MessageBox is

System.Collections.Generic.List`1[System.String]

There is no table data, even though I know there are actual data points for me to be getting :\

Skitzafreak
  • 1,797
  • 7
  • 32
  • 51
  • @ParvathiKrishnan I saw that, tried it already, and got the same results as above. – Skitzafreak Jun 20 '18 at 17:16
  • 1
    Calling `ToString` on a list will give you the type name, not the entire contents of the list. You need to look around the list and display each item one by one. – DavidG Jun 20 '18 at 17:18
  • @DavidG Thank you! I wasn't sure what I was doing wrong was with how I was trying to display the list, or how I was doing the query. – Skitzafreak Jun 20 '18 at 17:20
  • You could replace `POList.ToString()` with `String.Join(",", POList)`. – NetMage Jun 20 '18 at 17:30

2 Answers2

3

POList.ToString() will display the name of the object type. You can use string.Join(",", POList.ToString()).

You can also define an extension method StringJoin() to show your list as string if you are going to use it frequently.

Here is an extension method that you can use:

public static StringExtensions
{
public static string StringJoin(this IEnumerable<string> strings, string seperator)
        {
            if (strings == null) return null;
            return string.Join(seperator, strings);
        }
}
vendettamit
  • 14,315
  • 2
  • 32
  • 54
1

Since this was a vb.net question, I translated @vendettamit 's excellent answer for the benifit of future vb readers.

Public Module StringExtensions
    <Extension()>
    Public Function StringJoin(MyStrings As IEnumerable(Of String), separator As String) As String
        If MyStrings Is Nothing Then
            Return Nothing
        Else
            Return String.Join(separator, MyStrings)
        End If
    End Function
End Module

To use the extension...

Dim l As New List(Of String) From {"Mathew", "Mark", "Luke", "John"}
Dim s As String = l.StringJoin(", ")
Debug.Print(s)
Mary
  • 14,926
  • 3
  • 18
  • 27