2011-04-27 17 views
3

我正在構建一個RSS閱讀器,其中只有來自各種作者的最新帖子纔會加載,這意味着每個源博客只有一個帖子。下面的一段代碼在列表框中產生一排按鈕,每個按鈕都將文章的名稱和文章的發佈日期作爲文本,並在點擊時指向博客的鏈接。有太多的按鈕,因爲它使每個發佈一個。查詢具有不同來源的條目?

我想知道如何創建IEnumerable blogPosts只有對象博客其中BlogName是不同的。我不知道它是否應該是一個更精煉的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

那你現在得到什麼?沒有人會花時間爲你調試你的程序。你必須要問一個具體的問題。 – 2011-04-28 10:08:17

+0

這很多工作來加載列表框中的一排按鈕與BlogName和BlogPub(發佈日期)作爲按鈕上的文本和BlogUrl鏈接出來。目前它爲每個發佈的帖子加載一個按鈕,但是我需要的是每個博客一個按鈕。我不知道如何爲BlogName實現Distinct(或實現它)來影響整個blogPost。 – 2011-04-28 19:19:23

回答

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(); 
    } 
} 
+0

xxxooo!這就像一個魅力。一件事就是我需要將不同的blogPosts添加到列表框中,就像'this.blogListBox.ItemsSource = blogPosts.Distinct(new BlogNameComparer());'這對我來說是非常有用的東西;再次感謝。 – 2011-04-29 16:43:41

相關問題