Reputation: 148
I am working with a method using LINQ to XML to return a string.This is the XML
<data name="lnkViewResultResource1.Text" xml:space="preserve">
<value>View results</value>
</data>
<data name="lnkVoteResource1.Text" xml:space="preserve">
<value>Vote</value>
</data>
<data name="number of results" xml:space="preserve">
<value>You already {0} voted in this poll {1}</value>
</data>
(I want to take the "name" attribute) This is my method:
Public Shared Function getlabel(ByVal filename As String, ByVal valuetrans As String) As String
Dim label = From l In XElement.Load(filename).Elements("data") Where l.Element("value").Value = valuetrans Select (l.Attribute("name").Value).First
Return label.ToString
End Function
And this is returning this :
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]
I googled and used following this link FirstOrDefault(), but it didn't work in my case. Any ideas?
Thanks in advance,
Alf.
Upvotes: 0
Views: 149
Reputation: 3524
I think it's easier to use chaining:
var result = XElement.Load(filename).Elements("data")
.First(l => l.Element("value").Value == valuetrans )
.Attribute("name")
Upvotes: 0
Reputation: 8916
You can do it your way also, but you have to wrap the whole linq query in parens to do the first correctly:
Public Shared Function getlabel(ByVal filename As String, ByVal valuetrans As String) As String
Dim label = (From l In XElement.Load(filename).Elements("data")
Where l.Element("value").Value = valuetrans
Select l.Attribute("name").Value).First
Return label.ToString
End Function
Upvotes: 1
Reputation: 14387
I think your call to 'First' is placed wrong, try this:
Dim query = From l In XElement.Load(filename).Elements("data") _
Where l.Element("value").Value = valuetrans _
Select (l.Attribute("name").Value)
Dim label = query.First()
Return label.ToString()
Upvotes: 1