none
Convert to string Linq result

    Question

  • Hello Everyone,


    I have a little problem regarding with Linq to string convertion. Here is my Linq,

    Dim queryRes = From n In Me.TempTableInterest.AsEnumerable _
                               Where n.Field(Of Integer)("ident_id") = 2 _
                               Select n.Field(Of String)("source")

    And I want the result to be converted to string, something like,

    Dim strLinqRes As String = queryRes("source").ToString

    I know that code is incorrect but I just wanted to illustrate it 'cause I am not good in paragraph explanation :)

    Thanks you in advance.

    Friday, July 27, 2012 7:22 AM

Answers

  • I assume ident_id is unique, so you won't have two rows with that value, right?

          Dim source = Aggregate n In Me.TempTableInterest.AsEnumerable _
                                 Where n.Field(Of Integer)("ident_id") = 2 _
                                 Select n.Field(Of String)("source") _
                                 Into [Single]()
    
    If it's possible that the value is not found, replace Single by SingleOrDefault. Then variable source will be Nothing if 2 is not found.


    Armin

    Friday, July 27, 2012 12:05 PM

All replies

  • I assume ident_id is unique, so you won't have two rows with that value, right?

          Dim source = Aggregate n In Me.TempTableInterest.AsEnumerable _
                                 Where n.Field(Of Integer)("ident_id") = 2 _
                                 Select n.Field(Of String)("source") _
                                 Into [Single]()
    
    If it's possible that the value is not found, replace Single by SingleOrDefault. Then variable source will be Nothing if 2 is not found.


    Armin

    Friday, July 27, 2012 12:05 PM
  • Hi Charlie;

    In your query result queryRes is a collection of strings. Are you saying that you want one single string to be returned so that the return value looks something like the following?

    "Element 1, Element 2, ... , Element n"

    or 

    "Element 1 Element 2 ...  Element n"

    or some other form?

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, July 27, 2012 2:21 PM
  • Your query expression when execute may return more than one string.  If you only care about the first, then you can use this code:

    Dim strLinqRes As String = queryRes.FirstOrDefault()

    That will return the first string in the table.  If you need to process more than one, then you can use For Each:

    For Each strLinqRes As String In queryRes
        'Do something with strLinqRes here
    Next

    Friday, July 27, 2012 10:05 PM
  • Hello Sir Fernando,

    Sorry for the late reply. Yes Sir I want a single string to be returned that's why I'm using ("ident_id") on my Linq.

    Monday, July 30, 2012 1:21 AM
  • Hi Charlie;

    Try the query like this. This will return the one and only result or Nothing if non were found.

    Dim queryRes = (From n In Me.TempTableInterest.AsEnumerable _
                    Where n.Field(Of Integer)("ident_id") = 2 _
                    Select n.Field(Of String)("source")).SingleOrDefault()

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, July 30, 2012 1:03 PM

  • Dim queryRes = (From n In Me.TempTableInterest.AsEnumerable _
                    Where n.Field(Of Integer)("ident_id") = 2 _
                    Select n.Field(Of String)("source")).SingleOrDefault()

     

    I agree, but that's what he already had after the first reply.

    Armin

    Monday, July 30, 2012 1:11 PM
  • I assume ident_id is unique, so you won't have two rows with that value, right?

          Dim source = Aggregate n In Me.TempTableInterest.AsEnumerable _
                                 Where n.Field(Of Integer)("ident_id") = 2 _
                                 Select n.Field(Of String)("source") _
                                 Into [Single]()
    If it's possible that the value is not found, replace Single by SingleOrDefault. Then variable source will be Nothing if 2 is not found.


    Armin


    This is what I am talking about ^^

    Thanks Sir Armin...
    Wednesday, August 01, 2012 1:23 AM