我正在构建一个RSS阅读器,其中只有来自各种作者的最新帖子才会加载,这意味着每个源博客只有一个帖子。下面的代码在列表框中生成一行按钮,每个按钮都具有博客的名称和帖子的发布日期作为文本,以及单击时指向博客的链接。按钮太多,因为它为每个发布创建了一个按钮。
我想知道如何使用BlogName不同的对象Blogs创建IEnumerable blogPosts。我不知道它是否应该是一个更精致的Linq查询(我一直在尝试这在许多变化中无济于事)或循环通过blogPosts以某种方式使所有那些博客与Dups作为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; }
}
答案 0 :(得分:1)
您可以使用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();
}
}