Question

I have an XElement like this:

<Table1>
   <Field1>1</Field1>
   <Field2>example1</Field1>
   <Field1></Field1>
   <Field4>540</Field1>
</Table1>

What I want to get is two strings like this:

String1="Field1,Field2,Field3,Field4"
String2="1,example1,,540"

What I have now is:

Dim queryF = From p In elem.Elements _
         Select p.Name 
Dim queryV = From p In elem.Elements _
        Select p.Value

I know that i can convert queryF and queryV to list or array and then produce my strings but I was wondering if I can do it directly in the same LINQ

Thanks in advance

Was it helpful?

Solution

You cannot control the type of the data that is returned by the LINQ query. The type is determined by the LINQ provider (in this case, the LINQ to XML provider). What you do know, however, is that whatever it returns, it will always implement IEnumerable. Since the String.Join method has an overload which takes an IEnumerable parameter, you don't need to first convert it to an array:

Dim elem As XElement = <Table1>
                           <Field1>1</Field1>
                           <Field2>example1</Field2>
                           <Field1></Field1>
                           <Field4>540</Field4>
                       </Table1>
Dim string1 As String = String.Join(",", From p In elem.Elements Select p.Name)
Dim string2 As String = String.Join(",", From p In elem.Elements Select p.Value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top