具有不同来源的查询条目?

发布于 2024-11-03 16:52:53 字数 2036 浏览 0 评论 0原文

我正在构建一个 RSS 阅读器,其中只会加载来自不同作者的最新帖子,这意味着每个源博客只有一篇帖子。以下代码在列表框中生成一行按钮,每个按钮都以文本形式显示博客名称和帖子的发布日期,单击后会显示指向博客的链接。按钮太多,因为每次发布都会制作一个按钮。

我想知道如何仅使用 BlogName 不同的对象博客创建 IEnumerable blogPosts。我不知道它是否应该是一个更精致的 Linq 查询(我一直在尝试许多变体但无济于事)或通过 blogPosts 循环以某种方式取消所有那些以重复作为 BlogNames 的博客。

            private void client_DownloadStringCompleted(object sender,
          DownloadStringCompletedEventArgs e)
        {
          if (e.Error == null)
          {
            //declare the document xml to parse
            XDocument LABlogs = XDocument.Parse(e.Result);

            //declare the namespace of the xml to parse
            XNamespace xmlns = "http://www.w3.org/2005/Atom";

                //set the variable to collect the content for the buttons in the blogList ListBox
                //I'm parsing two nodes on the same level, then thier descendants both element and attribute
                var blogPosts = from source in LABlogs.Descendants(xmlns + "source")
                                from entry in LABlogs.Descendants(xmlns + "entry")

                                //someplace here I want to filter to where the source is distinct
                                select new Blog

                                { 
                                    //parsing the feed to get the button properties
                                    BlogName =(string)source.Element(xmlns + "title").Value,
                                    BlogUrl = (string)source.Element(xmlns + "link").Attribute("href").Value,
                                    BlogPub = (string)entry.Element(xmlns + "published").Value
                                };

                //add the var containing the button properties to the ListBox
                this.blogListBox.ItemsSource = blogPosts;

          }
        }
}
         public class Blog
          {
            public string BlogName { get; set; }
            public string BlogUrl { get; set; }
            public string BlogPub { get; set; }
          }

I'm building an RSS reader where only the most recent post from a variety of authors will load, meaning, each source blog only has one post. The following piece of code produces a row of buttons in a list box each one having the name of the blog and the publishing date of the post as text, and a link to the blog when clicked. There are too many buttons because it makes one for each publishing.

I would like to know how to create the IEnumerable blogPosts with only object Blogs where BlogName is distinct. I don't know if it should be a more refined Linq query (I've been trying this in many variations to no avail) or a loop through blogPosts to somehow nullify all those Blogs with dups as BlogNames.

            private void client_DownloadStringCompleted(object sender,
          DownloadStringCompletedEventArgs e)
        {
          if (e.Error == null)
          {
            //declare the document xml to parse
            XDocument LABlogs = XDocument.Parse(e.Result);

            //declare the namespace of the xml to parse
            XNamespace xmlns = "http://www.w3.org/2005/Atom";

                //set the variable to collect the content for the buttons in the blogList ListBox
                //I'm parsing two nodes on the same level, then thier descendants both element and attribute
                var blogPosts = from source in LABlogs.Descendants(xmlns + "source")
                                from entry in LABlogs.Descendants(xmlns + "entry")

                                //someplace here I want to filter to where the source is distinct
                                select new Blog

                                { 
                                    //parsing the feed to get the button properties
                                    BlogName =(string)source.Element(xmlns + "title").Value,
                                    BlogUrl = (string)source.Element(xmlns + "link").Attribute("href").Value,
                                    BlogPub = (string)entry.Element(xmlns + "published").Value
                                };

                //add the var containing the button properties to the ListBox
                this.blogListBox.ItemsSource = blogPosts;

          }
        }
}
         public class Blog
          {
            public string BlogName { get; set; }
            public string BlogUrl { get; set; }
            public string BlogPub { get; set; }
          }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-11-10 16:52:53

您可以使用 Distinct Linq 方法,传递 IEqualityComparer:

    private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            //declare the document xml to parse
            XDocument LABlogs = XDocument.Parse(e.Result);

            //declare the namespace of the xml to parse
            XNamespace xmlns = "http://www.w3.org/2005/Atom";

            //set the variable to collect the content for the buttons in the blogList ListBox
            //I'm parsing two nodes on the same level, then thier descendants both element and attribute
            var blogPosts = from source in LABlogs.Descendants(xmlns + "source")
                            from entry in LABlogs.Descendants(xmlns + "entry")
                            //someplace here I want to filter to where the source is distinct
                            select new Blog
                            {
                                //parsing the feed to get the button properties
                                BlogName = (string)source.Element(xmlns + "title").Value,
                                BlogUrl = (string)source.Element(xmlns + "link").Attribute("href").Value,
                                BlogPub = (string)entry.Element(xmlns + "published").Value
                            };

            // ******************************** //
            // >>>> here is the Distinct code <<<<
            // ******************************** //
            blogPosts.Distinct(new BlogNameComparer());

            //add the var containing the button properties to the ListBox
            this.blogListBox.ItemsSource = blogPosts;
        }
    }

相等比较器的代码:

public class BlogNameComparer : IEqualityComparer<Blog>
{
    bool IEqualityComparer<Blog>.Equals(Blog x, Blog y)
    {
        if (x == null) return y == null;
        if (y == null) return false;
        return string.Equals(x.BlogName, y.BlogName);
    }
    int IEqualityComparer<Blog>.GetHashCode(Blog obj)
    {
        if (obj == null) return 0;
        return obj.BlogName.GetHashCode();
    }
}

You can use the Distinct Linq method, passing a IEqualityComparer:

    private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            //declare the document xml to parse
            XDocument LABlogs = XDocument.Parse(e.Result);

            //declare the namespace of the xml to parse
            XNamespace xmlns = "http://www.w3.org/2005/Atom";

            //set the variable to collect the content for the buttons in the blogList ListBox
            //I'm parsing two nodes on the same level, then thier descendants both element and attribute
            var blogPosts = from source in LABlogs.Descendants(xmlns + "source")
                            from entry in LABlogs.Descendants(xmlns + "entry")
                            //someplace here I want to filter to where the source is distinct
                            select new Blog
                            {
                                //parsing the feed to get the button properties
                                BlogName = (string)source.Element(xmlns + "title").Value,
                                BlogUrl = (string)source.Element(xmlns + "link").Attribute("href").Value,
                                BlogPub = (string)entry.Element(xmlns + "published").Value
                            };

            // ******************************** //
            // >>>> here is the Distinct code <<<<
            // ******************************** //
            blogPosts.Distinct(new BlogNameComparer());

            //add the var containing the button properties to the ListBox
            this.blogListBox.ItemsSource = blogPosts;
        }
    }

Code of the equality comparer:

public class BlogNameComparer : IEqualityComparer<Blog>
{
    bool IEqualityComparer<Blog>.Equals(Blog x, Blog y)
    {
        if (x == null) return y == null;
        if (y == null) return false;
        return string.Equals(x.BlogName, y.BlogName);
    }
    int IEqualityComparer<Blog>.GetHashCode(Blog obj)
    {
        if (obj == null) return 0;
        return obj.BlogName.GetHashCode();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文