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; }
}
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With