0

Getting this weird LINQ error.

title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String

Here is the code I have:

if (Request.QueryString["Keywords"] != null){
        string keywords = Request.QueryString["Keywords"];
            string myAppID = "HIDDEN";
            var xml = XDocument.Load("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=" + myAppID + "&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=" + keywords + "&paginationInput.entriesPerPage=5");
            XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
            var titles = from item in xml.Root.Descendants(ns + "title")
                              select new{
                                  title = xml.Descendants(ns + "title").Select (x => x.Value),
                              };
        foreach (var item in titles){
                Label1.Text += item;
            } 
        }

Here what the XML looks like:

<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<searchReslut count="5">
<item>
    <title></title>
</item>
<item>
    <title></title>
</item>
<item>
    <title></title>
</item>

Trying to get it to output correctly.

allencoded
  • 7,015
  • 17
  • 72
  • 126

3 Answers3

3

Instead of

title = xml.Descendants(ns + "title").Select (x => x.Value)

change to

title = item.Value

EDIT as ChrisGessler suggests, but with my suggestion:

if (Request.QueryString["Keywords"] != null)
{
    string keywords = Request.QueryString["Keywords"];
    string myAppID = "HIDDEN";
    var xml = XDocument.Load(/* snip */);
    XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
    var titles = xml.Root.Descendants(ns + "title").Select(x => x.Value);
    Label1.Text = String.Join(null, titles);
}
Codesleuth
  • 10,321
  • 8
  • 51
  • 71
  • Couldn't the select new part be removed as well? select item.Value – Chris Gessler Jun 27 '12 at 15:48
  • Well if we were to look at the question as a challenge for optimization, then sure we could pull it apart and get much cleaner code. My answer was only to solve OP's problem, but feel free to post an answer with a rework :) – Codesleuth Jun 27 '12 at 15:50
  • question? How can I output it a array? – allencoded Jun 27 '12 at 23:51
  • In the example I wrote, you can see that `xml.Root.Descendants(ns + "title").Select(x => x.Value)` returns an `IEnumerable`, which you can simply do `.ToArray()` to get an array. So: `titles.ToArray()` – Codesleuth Jun 28 '12 at 09:28
1

I'm thinking this:

var titles = from item in xml.Root.Descendants(ns + "title")                               
             select new{                                   
                title = xml.Descendants(ns + "title").Select (x => x.Value)}; 

Should be:

var titles = from item in xml.Root.Descendants(ns + "title")                               
             select item.Value);
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
0

quick snippet

.. as example when using mvc grid in a cshtml razor page..

..

 .RenderValueAs(
       item => @Html.ActionLink(
        (
         from tsrItem in (item.<yourColumnName>) 
         where tsrItem.<IdField> == item.<IdField> 
         select tsrItem.<desiredColumn>
        ).FirstOrDefault(),

.. hope it helps