简体   繁体   中英

Get a string out of Linq query result

I am trying to make a bot which search the web for a specific term and stuff like that, in this case the webpage is duckduckgo; I have the Linq code which is this:

        internal async Task<IEnumerable<SearchResult>> Search(string query)
    {
        string xml = await new WebClient()
        .DownloadStringTaskAsync($"http://api.duckduckgo.com/?q={query}&format=xml");
        var root = XElement.Parse(xml);

        return root.Descendants("RelatedTopic").Take(1)
            .Select(t => new SearchResult
            {
                Text = t.Element("Text")?.Value
            });
    }

    internal class SearchResult
    {
        internal string Text { get; set; }
    }

And I'm trying to convert the result into a string so that I can send the link to a chat as, well, a string. I'm not having any issues with the code overall; I just want to know how I'd do that. That's the whole code; nothing more is interfering with it.

使用Object.ToString()方法:

Text = t.Element("Text")?.Value?.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM