Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query entry with distinct source?

Tags:

xml

linq

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; }
          }
like image 874
Ele Munjeli Avatar asked Feb 24 '26 09:02

Ele Munjeli


1 Answers

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();
    }
}
like image 161
Miguel Angelo Avatar answered Feb 25 '26 23:02

Miguel Angelo