具有不同来源的查询条目?
我正在构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Distinct Linq 方法,传递 IEqualityComparer:
相等比较器的代码:
You can use the Distinct Linq method, passing a IEqualityComparer:
Code of the equality comparer: