使用anglesharp linq查询获取Href属性

Gue*_*lla 3 c# linq anglesharp

我试图了解如何使用anglesharp。

我根据示例(https://github.com/AngleSharp/AngleSharp)制作了此代码:

        // Setup the configuration to support document loading
        var config = Configuration.Default.WithDefaultLoader();
        // Load the names of all The Big Bang Theory episodes from Wikipedia
        var address = "http://store.scramblestuff.com/";
        // Asynchronously get the document in a new context using the configuration
        var document = await BrowsingContext.New(config).OpenAsync(address);
        // This CSS selector gets the desired content
        var menuSelector = "#storeleft a";
        // Perform the query to get all cells with the content
        var menuItems = document.QuerySelectorAll(menuSelector);
        // We are only interested in the text - select it with LINQ
        var titles = menuItems.Select(m => m.TextContent).ToList();

        var output = string.Join("\n", titles);

        Console.WriteLine(output);
Run Code Online (Sandbox Code Playgroud)

这按预期工作,但现在我想访问该Href属性,但我无法执行此操作:

var links = menuItems.Select(m => m.Href).ToList();
Run Code Online (Sandbox Code Playgroud)

当我查看调试器时,我可以在结果视图中看到HtmlAnchorElement可枚举对象具有 Href 属性,但我显然没有尝试正确访问它。

文档中的所有示例都没有显示正在访问的属性,所以我想它很简单,不需要显示,但我不知道如何去做。

谁能告诉我应该如何访问锐角的 html 属性?

编辑:

当我将它转换为正确的类型时,这有效

foreach (IHtmlAnchorElement menuLink in menuItems)
        {
            Console.WriteLine(menuLink.Href.ToString());
        }
Run Code Online (Sandbox Code Playgroud)

我将如何将其编写为类似于 titles 变量的 Linq 语句?

tof*_*fi9 6

har07 答案的替代方法:

var menuItems = document.QuerySelectorAll(menuSelector).OfType<IHtmlAnchorElement>();
Run Code Online (Sandbox Code Playgroud)