views:

814

answers:

1

There's something wrong with my code or I'm just not understanding this fully. I have the following code that runs a query that MAY contain more than one RIGHT attribute and want to store each within an array for later fetching:

        var members = from myList in o_data.Descendants(bp + "Reaction")
                            select new
                            {
                                participant = myList.Element(bp + "RIGHT").Attribute(rdf + "resource").Value,
                            };

        return members.ToArray(); // I this the right conversion (to store all attributed values to array)?

Since they don't have collections in Silverlight, I'm trying to just return an Array... when I call the function like this:

FunctionName.GetValue(0).ToString();

It returns { participant = #(ValueOfAttribute) }

I just want to return the actual value, not the curley braces or "particpant =" What's going on here?

+4  A: 

The curly brackets are there because the ToString implementation of the anonymous type that you are creating in the linq query puts them there.

Here's a ToString implementation from an anonymous type:

public override string ToString()
{
    StringBuilder builder = new StringBuilder();
    builder.Append("{ test = ");
    builder.Append(this.<test>i__Field);
    builder.Append(" }");
    return builder.ToString();
}

If you only want to return the value of 'participant', remove all the anonymous type stuff and just select that value:

var members = from myList in o_data.Descendants(bp + "Reaction")
  select myList.Element(bp + "RIGHT").Attribute(rdf + "resource").Value;
return members.ToArray();

Finally, if you want to select multiple values per element (your sample code has a dangling comma that implies this), define a named type and create instances of that in your query. You can then refer to the properties of the type outside the query method, because the array will be an array of instances of that type, rather than an array of object.

mackenir
Excellent, thanks for the extra information
dotnetdvlpr