gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:42:28 26 4
gpt4 key购买 nike

我正在构建一个 RSS 阅读器,其中只会加载来自不同作者的最新帖子,也就是说,每个源博客只有一篇帖子。下面的代码片段在列表框中生成一行按钮,每个按钮都以文本形式显示博客名称和帖子的发布日期,并在单击时链接到博客。按钮太多,因为它为每个发布制作一个按钮。

我想知道如何创建仅包含 BlogName 不同的对象 Blogs 的 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; }
}

最佳答案

您可以使用 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();
}
}

关于xml - 具有不同来源的查询条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com